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
思路:分割字符串然后分别马拉车
ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
#define maxx 1100
char str[maxx];
char str1[2*maxx];
char s[maxx];
int p[2*maxx];int h;
int check(char a)
{
if(a=='A'||a=='H'||a=='I'||a=='M'||a=='O'||a=='T'||a=='U'||a=='V'||a=='W'||a=='X'||a=='Y')
return 1;
else
return 0;
}
int Manacher(int k)
{
h=0;
str1[h++]='@';
for(int i=0; i<k; i++)
{
str1[h++]='#';
str1[h++]=str[i];
}
str1[h]='#';
int mx=0;
int idx=0;
p[0]=1;
for(int i=1; i<h; i++)
{
p[i]=mx>i?min(p[2*idx-i],mx-i):1;
while(str1[i+p[i]]==str1[i-p[i]])
p[i]++;
if(i+p[i]>mx)
{
mx=i+p[i];
idx=i;
}
}
int ans=0;
for(int i=0;i<h;i++)
ans=max(ans,p[i]);
return ans-1;
}
void print(){
for(int i=0;i<h;i++)
printf("%d ",p[i]);
cout<<endl;
}
int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
memset(p,0,sizeof(p));
memset(str,0,sizeof(str));
memset(str1,0,sizeof(str1));
gets(s);
int nn=strlen(s);
s[nn]='*';
int k=0;
int maxn=0;
for(int i=0;i<=nn;i++)
{
if(check(s[i])==0)
{
int g=Manacher(k);
maxn=max(maxn,g);
k=0;
memset(p,0,sizeof(p));
memset(str,0,sizeof(str));
memset(str1,0,sizeof(str1));
}
else
str[k++]=s[i];
}
printf("%d\n",maxn);
}
return 0;
}