Mirrored String II 【最长回文子串】

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

#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int M =1e5;
bool pan(char ch){
    if(ch=='A'||ch=='H'||ch=='I'||ch=='M'||ch=='O'||ch=='T'||ch=='U'||ch=='V'||ch=='W'||ch=='X'||ch=='Y') return false;
    return true;
}
char s[M];
int main(){
    int t;scanf("%d",&t);
    getchar();
    while(t--){

        gets(s);    
        int len=strlen(s);
        int i,j;  int ans=0;  
        for(i=0;i<len;i++){
            if(pan(s[i])) continue; // 在几个地方加个判断就行了
            for(j=1;i+j<len&&i-j>=0&&s[i+j]==s[i-j]&&!pan(s[i+j]);j++) ;//aba型
            ans=max(ans,2*(j-1)+1);

           for(j=1;i+j<len&&i-j+1>=0&&s[i+j]==s[i-j+1]&&!pan(s[i+j]);j++) ;//abba型
            ans=max(ans,2*(j-1)); 
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值