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
Code:
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
getline(cin,s);
s="@"+s;
s=s+"#";
int maxn=0;
for(int i=1;i<s.size()-1;i++){
string str = string(1, s[i]);
for (int j = i - 1, k = i + 1; j > 0 && k < s.size(); j--, k++) {
if (s[j] == s[k])
str = str + s[k], str = s[j] + str;
else
break;
}
maxn=max(maxn,int(str.size()));
str ="";
for (int j = i, k = i+1; j > 0 && k < s.size(); j--, k++) {
if (s[j] == s[k])
str = str + s[k], str = s[j] + str;
else
break;
}
maxn=max(maxn,int(str.size()));
// cout<<str<<endl;
}
cout<<maxn<<endl;
}