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".
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.
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.
3 IOIKIOOI ROQ WOWMAN
4 1 3
#include<iostream>
#include<cstring>
#include <cstdio>
#include <cmath>
using namespace std;
char str1[110010],str2[110010*2],str[110010];
int n,a[110010*2];
int min(int x,int y)
{
return x>y?y:x;
}
void bian(int len)
{
int i;
n=1;
str2[0]='$';
for(i=0;i<len;i++)
{
str2[n++]='#';
str2[n++]=str1[i];
}
str2[n]='#';
}
int solve()
{
int max=-1;
int i,k,p=0;
for(i=1;i<n;i++)
{
if(i<p)
a[i]=min(a[k*2-i],p-i);
else
a[i]=1;
for(;str2[i+a[i]]==str2[i-a[i]];a[i]++)
;
if(i+a[i]>p)
{
k=i;
p=i+a[i];
}
if(max<a[i])
max=a[i];
}
return max;
}
int main()
{
int len,maxx,t;
cin>>t;
char judge[11]={'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
while(t--)
{
scanf("%s",str);
len=strlen(str);
getchar();
maxx=-1;
int wei=0;
for(int i=0;i<len;i++)
{
int flag=0;
for(int j=0;j<11;j++)
if(str[i]==judge[j]) {flag=1;break;}
if(flag==0)
{
str1[0]='\0';
wei=0;
}
else
{
str1[wei]=str[i];
wei++;
str1[wei]='\0';
// cout<<str1<<endl;
memset(str2,' ',sizeof(str2));
bian(strlen(str1));
maxx=max(solve(),maxx);
//cout<<maxx<<endl;
}
}
/*bian(len);
max=solve();*/
if(maxx==-1) cout<<"0"<<endl;
else
printf("%d\n",maxx-1);
}
return 0;
}