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

题意:

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

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 char s[1007];
 5 int main(){
 6     ios::sync_with_stdio(false);
 7     cin.tie(NULL);
 8     cout.tie(NULL);
 9     cin.getline(s+1,1005);
10     int ans=0;
11     int cnt=strlen(s+1);
12     if(cnt)
13         ans=1;
14     for(int i=1;i<=cnt;++i){
15         int l=i-1,r=i+1;
16         while(1){
17             if(l<1||r>cnt)
18                 break;
19             if(s[l]==s[r]){
20                 ans=max(ans,r-l+1);
21                 --l;
22                 ++r;
23             }
24             else
25                 break;
26         }
27         l=i,r=i+1;
28         while(1){
29             if(l<1||r>cnt)
30                 break;
31             if(s[l]==s[r]){
32                 ans=max(ans,r-l+1);
33                 --l;
34                 ++r;
35             }
36             else
37                 break;
38         }
39     }
40     cout<<ans;
41     return 0;
42 }

 

 

 

posted @ 2019-09-26 00:55  sewage  阅读(120)  评论(0编辑  收藏  举报