CF Mirrored String II

I. Mirrored String II
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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

Copy

 
  

3
IOIKIOOI
ROQ
WOWMAN

Output

Copy

 
  

4
1
3


求最长的子回文字符串,又get了一个知识点。

将O(n^3)转化为O(n^2),子回文字符串的特点是他是连续的,那么我以母串的每一个字符为起点,看看他的回文串最长的长度,遍历一遍后找到最长的。

如何来找该起点串的最长回文长度?首先该回文串必然是从该起点开始的,那么我就用两个下标变量吗m,n,开始的时候m在起点,n在末尾,如果这个长度不行,那么n就向前移一个,再判断是否成立,直到找到回文串。
#include <iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<deque>
#include<stack>
using namespace std;
int check(char c)
{
    if(c=='A'||c=='H'||c=='I'||c=='M'||c=='O'||c=='T'||c=='U'||c=='V'||c=='W'||c=='X'||c=='Y')
        return 1;
    else
        return 0;
}
int main()
{
    char con[1009];
    int n,m,i,p,j,t,k;
    int ans,head=0;
    scanf("%d",&t);
    for(i=1; i<=t; i++)
    {
        ans=head=0;
        scanf("%s",con);
        k=strlen(con);
        for(j=0; j<k; j++)
        {
            ans=0;
            n=j;
            m=k-1;
            p=0;
            while(1)
            {
                if(n>m)
                    break;
                if(check(con[n])&&check(con[m])&&con[n]==con[m])
                {
                    ans++;
                    if(n==m)
                        break;
                    n++;
                    m--;
                }
                else
                {
                    p++;
                    ans=0;
                    n=j;
                    m=k-1-p;
                }
            }
            if(n==m)          //应当注意回文串长度为奇数还是偶数的判别
                ans=2*ans-1;
            else
                ans=ans*2;
            if(ans>head)
                head=ans;
        }
        printf("%d\n",head);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值