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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int mid;
int maxL = -;
string ss;
int main(){
getline(cin, ss);
int len = ss.length();
for(mid = ; mid < len; mid++){
int i, j;
i = mid; j = mid;
while(i >= && j < len && ss[i] == ss[j]){
i--; j++;
}
int tempL = j - i + - ;
if(tempL > maxL)
maxL = tempL;
i = mid; j = mid + ;
while(i >= && j <len && ss[i] == ss[j]){
i--; j++;
}
tempL = j - i + - ;
if(tempL > maxL)
maxL = tempL;
}
printf("%d", maxL);
cin >> mid;
return ;
}

总结:

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

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

上一篇:Windows运行


下一篇:周末没事干就看CSS JS Python ThinkPHP的书,照着例子运行就行,可以增强信心(www.delphihtmlcomponents.com 是神器,也可以帮助我学习。还有虚拟机运行Web)