最长回文子串(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
Copy
3
IOIKIOOI
ROQ
WOWMAN
Output
Copy
4
1
3
题解:暴力呀。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <math.h>
 6 #define PI acos(-1.0)
 7 using namespace std;
 8 bool judge(char arr)
 9 {
10     if(arr=='A'||arr=='H'||arr=='I'||arr=='M'||arr=='O'||arr=='T'||arr=='U'||arr=='V'||arr=='W'||arr=='X'||arr=='Y')
11         return false;
12     return true;
13 }
14 int main()
15 {
16     int i,j,n,k,m,kk;
17     char str[2001];
18     scanf("%d",&n);
19     while(n--)
20     {
21         int ans=0,pp;
22         scanf(" %s",&str);
23         for(i=0;i<strlen(str);i++)
24             for(j=i;j<strlen(str);j++)
25             {
26                 if(judge(str[j]))
27                     break;
28                 if(str[j]==str[i])
29                 {
30                     m=j;int sum=0;
31                     for(k=i;k<(i+j+1)/2;k++)
32                     {
33                         if(str[k]==str[m--])
34                             sum++;
35                     }
36                     if(sum==(j-i+1)/2)
37                         ans=max(ans,j-i+1);
38                 }
39 
40             }
41         printf("%d\n",ans);
42 
43     }
44     return 0;
45 }

 

posted @ 2018-04-26 21:14  左手边五十米  阅读(380)  评论(0编辑  收藏  举报