Manacher(最长镜面回文串)
I - O'My!
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".
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
3
IOIKIOOI
ROQ
WOWMAN
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(最长镜面回文串)的更多相关文章
- BZOJ 2565: 最长双回文串 [Manacher]
2565: 最长双回文串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1842 Solved: 935[Submit][Status][Discu ...
- BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)
BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...
- bzoj 2565: 最长双回文串 manacher算法
2565: 最长双回文串 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...
- 计蒜之道 初赛 第三场 题解 Manacher o(n)求最长公共回文串 线段树
腾讯手机地图 腾讯手机地图的定位功能用到了用户手机的多种信号,这当中有的信号的作用范围近.有的信号作用的范围则远一些.有的信号相对于用户在不同的方位强度是不同的,有的则是在不论什么一个方向上信号强度都 ...
- 【BZOJ2565】最长双回文串 Manacher
[BZOJ2565]最长双回文串 Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同).输入长度为 ...
- BZOJ2565 最长双回文串 【Manacher】
BZOJ2565 最长双回文串 Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"c ...
- [国家集训队]最长双回文串 manacher
---题面--- 题解: 首先有一个直观的想法,如果我们可以求出对于位置i的最长后缀回文串和最长前缀回文串,那么我们枚举分界点然后合并前缀和后缀不就可以得到答案了么? 所以我们的目标就是求出这两个数列 ...
- Manacher【p4555】 [国家集训队]最长双回文串
题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...
- BZOJ 2565 最长双回文串(manacher)
565: 最长双回文串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3343 Solved: 1692[Submit][Status][Discu ...
随机推荐
- HBase HA分布式集群搭建
HBase HA分布式集群搭建部署———集群架构 搭建之前建议先学习好HBase基本构架原理:https://www.cnblogs.com/lyywj170403/p/9203012.html 集群 ...
- 线程屏障CyclicBarrier实现原理
生产环境中,存在需要等待多个线程都达到某种状态后,才继续运行的情景.并发工具CyclicBarrier就能够完成这种功能.本篇从源码方面,简要分析CyclicBarrier的实现原理. 使用示例 pu ...
- Maya多版本下载与激活方法
目录 1. 安装激活说明 1. 安装激活说明 Maya2019:https://www.cnblogs.com/coco56/p/11425559.html Maya2017:https://www. ...
- maven_上传到私服,以及从私服下载
公司由于没有maven,自己又想用,于是乎,就自己搭了一个nexus 1.苦逼不多说,将本地jar文件上传到maven 需要在本机(客户端windows)中的maven中的setting.xml添加这 ...
- Vue 学习之 vue-router2
---恢复内容开始--- 一.路由的安装: npm安装 npm install vue-router --save 执行命令完成vue-router的安装,并在package.json中添加了vue- ...
- layui树形表格支持非异步和异步加载
layui树形表格支持非异步和异步加载. 仓库地址:https://gitee.com/uniqid/ 使用示例如下: <div class="uui-admin-common-bod ...
- LinkedList与ArrayList的区别(内部实现)
ArrayList的内部实现是基于内部数组Object[],所以从概念上讲,它更像数组: LinkedList的内部实现是基于一组连接的记录,所以,它更像一个链表结构,所以,它们在性能上有很大的差别. ...
- 036:DTL常用过滤器(5)
slice过滤器: 类似于 Python 中的切片操作.示例代码如下: {{ some_list|slice:"2:" }} 以上代码将会给 some_list 从 2 开始做切片 ...
- orm 查询数据库随机返回一条数据的解决办法用models.User.objests.all().order_by('?').first()
- php stristr()函数 语法
php stristr()函数 语法 作用:返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写.直线电机厂家怎么样? 语法:stristr(string,search,before_s ...