题意:
求给定字符的最长回文串
思路:
将非给定字符离散成不同的值即可
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN=210000;
char ch[]={'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
char ch2[]={'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y', '#'};
struct Manacher{
int Ma[MAXN*2];
int Mp[MAXN*2];
int Mx[MAXN*2];
int len;
double ave;
int l;
int ans;
void manachar(int s[],int len){
l=0;ans=0;
Ma[l++]='$';
Ma[l++]='#';
for(int i=0; i<len; i++)
{
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=0;
int mx=0,id=0;
int pp=strlen(ch2);
for(int i=0; i<l; i++)
{
ave++;
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]]){
Mp[i]++;
ave++;
}
if(i+Mp[i]>mx)
{
mx=i+Mp[i];
id=i;
}
Mx[i]=mx;
for(int j=0;j<pp;j++)
if(Ma[i]==ch2[j]){
ans=max(ans,Mp[i]-1);break;
}
}
ave/=len;
}
};
Manacher man;
char buf[MAXN];
int buff[MAXN];
int T;
int main(){
cin>>T;
while(T--){
getchar();
scanf("%s",buf);
int len=strlen(buf);
int len2=strlen(ch);
int ppp=128;
bool flag;
for(int i=0;i<len;i++){
flag=0;
for(int j=0;j<len2;j++)
if(buf[i]==ch[j]){
flag=1;
break;
}
if(!flag)
buf[i]=++ppp;
}
for(int i=0;i<len;i++)
buff[i]=buf[i];
man.manachar(buff,len);
cout<<man.ans<<endl;
}
}
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