Gym_101350I_Mirrored String II_回文字符串(马拉车Manacher)

来源:互联网 发布:中国域名注册官网 编辑:程序博客网 时间:2024/06/16 21:19




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
3IOIKIOOIROQWOWMAN
Output
413


题意:给你一串字符串,求这串字符里由限定的字符所组成的字符串中最长的回文串的长度。

解:马拉车直接做,只是需要处理下非限定的字符。

我是将符合规定的字符一个个拆开进行求回文串长度,比较遍历后找出最大的值输出。毕竟字符串长度才1000。

代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#define MANX 1101using namespace std;char str1[MANX],str0[MANX],str[MANX<<1];int p[MANX<<1],len;void start(){    str[0]='$',str[1]='#';    len=strlen(str0);    for(int i=0;i<len;i++)    {        str[i*2+2]=str0[i];        str[i*2+3]='#';    }    str[len*2+2]='\0';}int solve(){    memset(p,0,sizeof(p));    int mx=0,id,ans=1;    for(int i=1;i<len*2+2;++i)    {        if(mx>i)p[i]=min(p[2*id-i],mx-i);        else p[i]=1;        for(;str[i-p[i]]==str[i+p[i]];p[i]++);        if(p[i]+i>mx)mx=p[i]+i,id=i;        if(p[i]>ans)ans=p[i];    }    return --ans;}//A, H, I, M, O, T, U, V, W, X, Yint main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(str,0,sizeof(str));        memset(str1,0,sizeof(str1));        memset(str0,0,sizeof(str0));        scanf("%s",str1);        int len1=strlen(str1);        len=0;int ans=-99999999;        for(int i=0;i<len1;i++)        {            if(str1[i]=='A'||str1[i]=='H'||str1[i]=='I'||str1[i]=='M'||str1[i]=='O'||str1[i]=='U'||str1[i]=='V'               ||str1[i]=='W'||str1[i]=='X'||str1[i]=='Y'||str1[i]=='T')               {                   str0[len]=str1[i];//将符合规定的字符串给待处理的str0;                    len++;               }               else               {                   start();                    ans=max(ans,solve());                    memset(str0,0,sizeof(str0));//清零重新处理                    memset(str,0,sizeof(str));                    len=0;               }        }        start();//最后处理下        ans=max(ans,solve());               printf("%d\n",ans);    }    //cout << "Hello world!" << endl;    return 0;}


1 0