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. 读取FTP上的excel文件,并写入数据库

    今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...

  2. 脚本_修改 Linux 系统的最大打开文件数量

    #!bin/bash#作者:liusingbon#功能:修改 Linux 系统的最大打开文件数量#追加两行配置参数到文件/etc/security/limits.conf的末尾,修改最大打开文件数量为 ...

  3. 模块(os模块)

    一.模块 一个python文件就是一个模块. 模块可分为: 1.标准模块:python自带的模块是标准模块,可以直接import进行使用的. eg:import json.import random. ...

  4. iconv 转换文件的编码格式

    1.命令功能 icnov用于转换文件的编码格式 linux默认中没有icnov文件,需要自己安装http://www.gnu.org/software/libiconv/. (1)下载libiconv ...

  5. Wannafly挑战赛27 C蓝魔法师

    链接Wannafly挑战赛27 C蓝魔法师 给出一棵树,求有多少种删边方案,使得删后的图每个连通块大小小于等于\(k\),\(n,k\leq 2*10^3\) 假设我们正在考虑\(i\)这个子树,那么 ...

  6. mybaties数据源配置类型(POOLED、JNDI、UNPOOLED)

    dataSource的类型可以配置成其内置类型之一,如UNPOOLED.POOLED.JNDI. 如果将类型设置成UNPOOLED,mybaties会为每一个数据库操作创建一个新的连接,并关闭它.该方 ...

  7. Spring---异步消息

    1.异步消息 1.1.目的:   为了  系统与系统  之间的通信: 1.2.概念: 异步消息  :消息的   发送者  无需 等待消息  接收者的处理及返回,甚至 无需 关心消息是否发送成功: 1. ...

  8. 运行 tensorboard

    使用下面命令总是报错: tensorboard --logdir=mylogdir tensorboard --logdir='./mylogdir' 正确命令 tensorboard --logdi ...

  9. Nginx-配置负载均衡实例

    配置负载均衡实例 实现效果: 配置负载均衡 实验代码 1) 首先准 备两个同时启动的 Tomcat 2) 在 nginx.conf 中进行配置 随着互联网信息的爆炸性增长,负载均衡(load bala ...

  10. [CSP-S模拟测试]:棋盘(数学+高精度)

    题目描述 在一个大小为$N\times N$的棋盘上,放置了$N$个黑色的棋子.并且,对于棋盘的每一行和每一列,有且只有一个棋子.现在,你的任务是再往棋盘上放置$N$个白色的棋子.显然,白色棋子不能与 ...