I - O'My!

Gym - 101350I

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
思路:将镜面字母拆分出来,一个一个求最长回文串,差点因为数组重复而runtime error 没过,还好发现了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = ;
char a[] , b[] ;
int p[] ;
int vis[] ;
char c[] = {'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
char d[] ; int cal(char *a , int lena)
{
b[] = '$';
b[] = '#';
int l = ;
for(int i = ; i < lena ; i++)
{
b[l++] = a[i];
b[l++] = '#';
}
b[l] = '\0';
// cout << b << endl ;
int mx = - ;
int mid , i ;
int len = ;
for(int i = ; i < l ; i++)
{
if(mx > i)
{
p[i] = min(p[mid * - i] , mx - i);
}
else
{
p[i] = ;
}
while(b[i-p[i]] == b[i+p[i]])
{
p[i]++;
}
if(mx < p[i] + i)
{
mid = i ;
mx = p[i] + i ;
}
len = max(len , p[i] - ); }
return len ;
} int main()
{
for(int i = ; i < ; i++)
{
int x = c[i] - 'A';
vis[x] = ;
}
int n ;
scanf("%d" , &n);
while(n--)
{ scanf("%s" , a);
int lena = strlen(a);
int ans = ;
int l = ;
for(int i = ; i < lena ; i++)
{
if(vis[a[i] - 'A'])
{
d[l++] = a[i];
}
if(!vis[a[i] - 'A'] || i == lena - )
{ if(l >= )
ans = max(ans ,cal(d , l));
d[l] = ;
l = ;
continue ;
}
}
printf("%d\n" , ans);
} return ;
}

Manacher(最长镜面回文串)的更多相关文章

  1. BZOJ 2565: 最长双回文串 [Manacher]

    2565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1842  Solved: 935[Submit][Status][Discu ...

  2. BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)

    BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...

  3. bzoj 2565: 最长双回文串 manacher算法

    2565: 最长双回文串 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...

  4. 计蒜之道 初赛 第三场 题解 Manacher o(n)求最长公共回文串 线段树

    腾讯手机地图 腾讯手机地图的定位功能用到了用户手机的多种信号,这当中有的信号的作用范围近.有的信号作用的范围则远一些.有的信号相对于用户在不同的方位强度是不同的,有的则是在不论什么一个方向上信号强度都 ...

  5. 【BZOJ2565】最长双回文串 Manacher

    [BZOJ2565]最长双回文串 Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同).输入长度为 ...

  6. BZOJ2565 最长双回文串 【Manacher】

    BZOJ2565 最长双回文串 Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"c ...

  7. [国家集训队]最长双回文串 manacher

    ---题面--- 题解: 首先有一个直观的想法,如果我们可以求出对于位置i的最长后缀回文串和最长前缀回文串,那么我们枚举分界点然后合并前缀和后缀不就可以得到答案了么? 所以我们的目标就是求出这两个数列 ...

  8. Manacher【p4555】 [国家集训队]最长双回文串

    题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...

  9. BZOJ 2565 最长双回文串(manacher)

    565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3343  Solved: 1692[Submit][Status][Discu ...

随机推荐

  1. MySQL语句之数据的增删改查

    1.插入记录insert语法:INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn); 也可以一次 ...

  2. 2019-3-9-通过-frp-开启服务器打开本地的-ZeroNet-服务器外网访问

    title author date CreateTime categories 通过 frp 开启服务器打开本地的 ZeroNet 服务器外网访问 lindexi 2019-03-09 11:47:4 ...

  3. Kintex 7五兄弟

    基KC705E 增强版 基于FMC接口的Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 接口卡(136) 本板卡是Xilinx公司芯片V5系列芯片设计信号处理板卡.由一 ...

  4. 树莓派 msmtp和mutt 的安装和配置

    1,安装mutt sudo apt-get install mutt 2,安装msmtp sudo apt-get install msmtp 3,设置mutt /etc/Muttrc # 系统全局设 ...

  5. [NOIP2009]最优贸易(图论)

    [NOIP2009]最优贸易 题目描述 CC 国有 \(n\) 个大城市和 \(m\) 条道路,每条道路连接这 \(n\) 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 \(m\ ...

  6. Java8 的一些新特性的学习理解

    近期在学习队列相关的一些知识,在学习过程中发现Iterable<T>接口中新增了两个新的方法,出于好奇,就想知道这是什么东东,干什么用的.俗话说:实践出真知,所以就有了以下反复的测试. 先 ...

  7. 为什么需要bootloader

    本文链接:https://blog.csdn.net/u012351051/article/details/50557899 受单片机和ARM7等小型CPU设备编程思维的影响,开始对嵌入式linux和 ...

  8. 【leetcode】870. Advantage Shuffle

    题目如下: 解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”.言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大 ...

  9. 迁移数据时 timestamp类型字段报错: 1067 - Invalid default value for 'login_time'

    MySQL数据库升级 8.0.13,原版本5.5:执行导出来的SQL文件时报错 1067 - Invalid default value for 'login_time' 原因:MySQL 5.6以后 ...

  10. JAVA WEB怎么实现大文件上传

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 改进后的代码不需要form标签,直 ...