1040 Longest Symmetric String (25分)
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<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
string str="",str1="";
int l=1,i,j,n,m;
getline(cin,str);
for(i=0;i<str.size();i++){
for(j=str.size();j>=0;j--){
int is=1;
if(j-i+1<l)break; //当l大于当前要进行判断的区间大小时,就直接退出,节省时间
str1=str.substr(i,j);
for(n=0,m=str1.size()-1;n!=str1.size()/2;n++,m--){
if(str1[n]!=str1[m]){
is=0;
break;
}
}
if(l<str1.size()&&is) l=str1.size();
}
}
cout<<l;
return 0;
}