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
题解:就是要找最长的回文镜像子串的长度,考虑奇偶两种情况暴力即可。
感受:做了1就感到2特别难,思维定势导致不敢去想这个题去怎么做~~~没想到是个暴力水题!!!
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#define mem(a,x) memset(a,x,sizeof(a));
using namespace std;
int odd[1010], even[1010];
int check(char s)
{
if (s == 'A' || s == 'H' || s == 'I' || s == 'M' || s == 'O')
return 1;
if (s == 'T' || s == 'U' || s == 'V' || s == 'W' || s == 'X' || s == 'Y')
return 1;
return 0;
}
int main()
{
int t;
string s;
cin >> t;
while (t--)
{
mem(odd, 0);
mem(even, 0);
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++)
{
if (check(s[i]))
{
odd[i]= 1; //奇数
int l = i - 1; //左移1.右移1
int r = i + 1;
while (l >= 0 && r < len)//while暴力
{
if (check(s[l]) && s[l] == s[r])
l--, r++, odd[i] += 2;
else
break;
}
if (check(s[i + 1] )&& s[i] == s[i + 1])//偶数
{
even[i]=2;//初始为2
int l=i-1;//左移1
int r=i+2;//右移2
while (l >= 0 && r < len)
{
if (check(s[l]) && s[l] == s[r])
l--, r++, even[i]+= 2;
else
break;
}
}
}
}
sort(odd, odd + len);
sort(even, even + len);
cout << max(odd[len - 1], even[len - 1]) << endl;
}
return 0;
}