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>
using namespace std;
int longest(string n)
{
int maxlen = 1, temp1, temp2;
for(int i=1; i<n.size(); i++)
{
temp1 = 0;
for(int j=0; i+j<n.size() && i-1-j>=0 && n[i+j]==n[i-1-j];j++)
{
temp1 += 2;
}
temp2 = 1;
for(int j=1; (i+j)<n.size() && (i-j)>=0 && n[i-j]==n[i+j]; j++)
{
temp2 += 2;
}
if(max(temp1, temp2) > maxlen)
maxlen = max(temp1, temp2);
}
return maxlen;
}
int main()
{
string n;
getline(cin, n);
cout << longest(n);
return 0;
}