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. Struts06---通配符的使用

    01.创建对应的login.jsp页面 <%@ page language="java" import="java.util.*" pageEncodin ...

  2. 条款14:在资源管理类中小型coping的行为

    首先假设对于一个mutex互斥器对象,有lock以及unlock两个函数可用: void lock(Mutex * pm); void unlock(Mutex * pm); 那么为了防止资源忘记被释 ...

  3. 多线程实现方式---实现Runnable接口

    多线程实现方式---实现Runnable接口 一个类如果需要具备多线程的能力,也可以通过实现java.lang.Runnable接口进行实现.按照Java语言的语法,一个类可以实现任意多个接口,所以该 ...

  4. 2017.11.4 Datasheet 查找替代料

  5. ADO.NET数据库访问技术(转)

    这几天的自学,现在总结一下关于C#中连接数据库的一些知识点: 1.使用Connection连接数据库的步骤: (1).添加命名空间 System.Data.SqlClient(注意:初学者经常会忘记) ...

  6. 《Drools7.0.0.Final规则引擎教程》第3章 3.2 KIE概念&FACT对象

    3.2.1 什么是KIE KIE(Knowledge Is Everything),知识就是一切的简称.JBoss一系列项目的总称,在<Drools使用概述>章节已经介绍了KIE包含的大部 ...

  7. *Fragment与Activity生命周期的协调

    本文出处:http://blog.csdn.net/think_soft/article/details/7272869 拥有Fragment的Activity的生命周期直接影响了其中的Fragmen ...

  8. Unity中使用柏林噪声生成地图

    孙广东  2017.3.27 http://blog.csdn.NET/u010019717 主要是利用Unity的 Mathf.PerlinNoise   函数(柏林噪声)的不同寻常的功能. htt ...

  9. 火狐下的GreaseMonkey和Chrome下的tampermonkey使用手记

    说明:GreaseMonkey的作用是让我们浏览器运行我们自己写的脚本,而且是在后台一直不断的运行,听着就让人兴奋. [ps:他们当然可以创建名单对哪些网站作用或者排除哪些站点]: 开始了:Greas ...

  10. 【解题报告】[动态规划]-PID69 / 过河卒

    原题地址:http://www.rqnoj.cn/problem/69 解题思路: 用DP[i][j]表示到达(i,j)点的路径数,则 DP[0][0]=1 DP[i][j]=DP[i-1][j]+D ...