A1040. Longest Symmetric String

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length no more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:

Is PAT&TAP symmetric?

Sample Output:

11

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 int mid;
 7 int maxL = -1;
 8 string ss;
 9 int main(){
10     getline(cin, ss);
11     int len = ss.length();
12     for(mid = 0; mid < len; mid++){
13         int i, j;
14         i = mid; j = mid;
15         while(i >= 0 && j < len && ss[i] == ss[j]){
16             i--; j++;
17         }
18         int tempL = j - i + 1 - 2;
19         if(tempL > maxL)
20             maxL = tempL;
21         i = mid; j = mid + 1;
22         while(i >= 0 && j <len && ss[i] == ss[j]){
23             i--; j++;
24         }
25         tempL = j - i + 1 - 2;
26         if(tempL > maxL)
27             maxL = tempL;
28     }
29     printf("%d", maxL);
30     cin >> mid;
31     return 0;
32 }
View Code

总结:

1、cin输入带空格的一行string: getline(cin, ss);

2、有两种回文:长度为奇数与长度为偶数的,需要分别讨论。aa、aaa、abb、abbb.

原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8563645.html