最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定
Is PAT&TAP symmetric?
,最长对称子串为s PAT&TAP s
,于是你应该输出11。输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
#include <bits/stdc++.h>
using namespace std;
string str1;
int main(){
int max1=0,max2=0;
getline(cin,str1);
int mid=str1.size()/2;
int lon=str1.size();
for(int i=0;i<=mid;i++){
int count=-1;
for(int j=0;j<=i;j++){
if(str1[i-j]==str1[i+j]){
count+=2;
}else{
break;
}
}
int count2=0;
if(mid!=0&&str1[i]==str1[i+1]){
for(int j=0;j<=i;j++){
if(lon>i+j+1&&str1[i-j]==str1[i+j+1]){
count2+=2;
}else{
break;
}
}
}
if(max2<count2){
max2=count2;
}
if(max1<count){
max1=count;
}
}
cout<<(max1>max2?max1:max2);
return 0;
}