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
题解:暴力呀。
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#define PI acos(-1.0)
using namespace std;
bool judge(char arr)
{
if(arr=='A'||arr=='H'||arr=='I'||arr=='M'||arr=='O'||arr=='T'||arr=='U'||arr=='V'||arr=='W'||arr=='X'||arr=='Y')
return false;
return true;
}
int main()
{
int i,j,n,k,m,kk;
char str[];
scanf("%d",&n);
while(n--)
{
int ans=,pp;
scanf(" %s",&str);
for(i=;i<strlen(str);i++)
for(j=i;j<strlen(str);j++)
{
if(judge(str[j]))
break;
if(str[j]==str[i])
{
m=j;int sum=;
for(k=i;k<(i+j+)/;k++)
{
if(str[k]==str[m--])
sum++;
}
if(sum==(j-i+)/)
ans=max(ans,j-i+);
} }
printf("%d\n",ans); }
return ;
}

最长回文子串(Mirrored String II)的更多相关文章

  1. [译+改]最长回文子串(Longest Palindromic Substring) Part II

    [译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...

  2. PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  5. LeetCode(5):最长回文子串

    Medium! 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 长度最长为1000. 示例: 输入: "babad" 输出: "bab&quo ...

  6. 转载:LeetCode:5Longest Palindromic Substring 最长回文子串

    本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...

  7. 最长回文子串--轻松理解Manacher算法

    最长回文子串这个问题的Manacher算法,看了很多博客,好不容易理解了,做一下记录. 这个算法的核心就是:将已经查询过的子字符串的最右端下标保存下来,在计算下标为i的回文字符串时,不需要从左右相邻的 ...

  8. 最长回文子串 C++实现 java实现 leetcode系列(五)

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...

  9. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  10. Manacher模板( 线性求最长回文子串 )

    模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...

随机推荐

  1. matlab将矩阵写入文件

    % %% date: 5/5/2017 % %% Author: Congbo Ma, Hu Wang % % write matrix to file function wrt_mat_to_fil ...

  2. [面试时]我是如何讲清楚TCP/IP是如何实现可靠传输的 转

    [面试时]我是如何讲清楚TCP/IP是如何实现可靠传输的 - shawjan的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/shawjan/article/det ...

  3. 旧书重温:0day2【8】狙击windows的异常处理实验

    现在进入0day2的第六章内容 其中第六章的书本内容我都拍成了图片格式放在了QQ空间中(博客园一张一传,太慢了)http://user.qzone.qq.com/252738331/photo/V10 ...

  4. R 之 rJava 包安装错误的解决方案

    前几天在Ubuntu上安装R中的xlsx包时一直卡在了rJava包的安装上,最终各种google都没能解决问题.直到最后,我回到了安装rJava时的错误记录....我用血的教训证明,错误日志是很重要很 ...

  5. Freemarker 自定义标签 实现TemplateDirectiveModel

    1 自定义标签需要实现TemplateDirectiveModel这个接口中的execute方法 实例代码如下 public class UserListDirective implements Te ...

  6. Sublime Text 3下Emmet使用技巧

    链接:http://jingyan.baidu.com/article/92255446a87900851648f4d6.html

  7. CodeForces - 484BMaximum Value(hash优化)

    个人心得:周测题目,一题没出,难受得一批.这个题目做了一个半小时还是无限WR,虽然考虑到了二分答案这个点上面了, 奈何二分比较差就想用自己的优化,虽然卡在了a=k*b+c,这里但是后面结束了这样解决还 ...

  8. WPF导学目录

    很早就知道WPF这个东西,做项目中没用到,也就没去整理学习.作为winForm的升级版,未来windows桌面应用程序的趋势,有些公司招聘需求中也会提到熟悉WPF,于是就整理学习了一下WPF. 主要参 ...

  9. JavaScript 冒号(:)详解

    1.switch语句分支 2.?:三元表达式的false 3.声明对象直接量的成员 4.声明标签 1和2相信地球人都知道吧?如果有人不知道,那我改成地球上的程序员都知道,哈哈 3.对象直接量我们也经常 ...

  10. JAVA反射调用方法

    1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotation.SetTable ...