【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))

题意:

输入一个包含空格的字符串,输出它的最长回文子串的长度。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
char s[1007];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin.getline(s+1,1005);
int ans=0;
int cnt=strlen(s+1);
if(cnt)
ans=1;
for(int i=1;i<=cnt;++i){
int l=i-1,r=i+1;
while(1){
if(l<1||r>cnt)
break;
if(s[l]==s[r]){
ans=max(ans,r-l+1);
--l;
++r;
}
else
break;
}
l=i,r=i+1;
while(1){
if(l<1||r>cnt)
break;
if(s[l]==s[r]){
ans=max(ans,r-l+1);
--l;
++r;
}
else
break;
}
}
cout<<ans;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11588579.html