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




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


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

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

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

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MANX 1101
using 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, Y
int 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值