Note: this is a harder version of Mirrored string I.
The gorillas have recently discovered that the image on the surface of the water is actually a reflection of themselves. So, the next thing for them to discover is mirrored strings.
A mirrored string is a palindrome string that will not change if you view it on a mirror.
Examples of mirrored strings are “MOM”, “IOI” or “HUH”. Therefore, mirrored strings must contain only mirrored letters {A, H, I, M, O, T, U, V, W, X, Y} and be a palindrome.
e.g. IWWI, MHHM are mirrored strings, while IWIW, TFC are not.
A palindrome is a string that is read the same forwards and backwards.
Given a string S of length N, help the gorillas by printing the length of the longest mirrored substring that can be made from string S.
A substring is a (possibly empty) string of characters that is contained in another string S. e.g. “Hell” is a substring of “Hello”.
Input
The first line of input is T – the number of test cases.
Each test case contains a non-empty string S of maximum length 1000. The string contains only uppercase English letters.
Output
For each test case, output on a line a single integer - the length of the longest mirrored substring that can be made from string S.
Example
Input
3
IOIKIOOI
ROQ
WOWMAN
Output
4
1
3
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int MAX = 1010;
char s[MAX];
int num[MAX];
int num1[MAX];
bool check(char s)
{
switch (s)
{
case 'A' : return true;
case 'H' : return true;
case 'I' : return true;
case 'M' : return true;
case 'O' : return true;
case 'T' : return true;
case 'U' : return true;
case 'V' : return true;
case 'W' : return true;
case 'X' : return true;
case 'Y' : return true;
default : return false;
}
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
memset(s,0,sizeof(s));
memset(num,0,sizeof(num));
memset(num1,0,sizeof(num1));
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++)
{
if(check(s[i]))
{
num[i]++;
}
else
{
continue;
}
int j=i-1,k=i+1;
while(j>=0&&k<len)
{
if((s[j]==s[k])&&check(s[j]))
{
num[i]++;
num[i]++;
}
else
{
break;
}
j--;
k++;
}
int w=i,l=i+1;
while(w>=0&&l<len)
{
if((s[w]==s[l])&&check(s[w]))
{
num1[i]++;
num1[i]++;
}
else
{
break;
}
w--;
l++;
}
}
// for(int i=0;i<len;i++)
// {
// printf("%d ",num1[i]);
// }
// cout<<endl;
sort(num,num+len,cmp);
sort(num1,num1+len,cmp);
printf("%d\n",max(num[0],num1[0]));
}
return 0;
}