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 ...
随机推荐
- struts2导入多个xml引入报错<include>
struts.xml <?xml version="1.0" encoding="UTF-8"?> <!-- 指定Struts 2配置文件的D ...
- windows 如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中
题目]如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中 一.概述 之前安装tensorflow的安装了anaconda并用它进行安装,anaconda ...
- 2018-09-20-weekly
Algorithm 最长有效括号 What 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. How 这里可以用栈来求解,需要定义个start变量来记录合法括号串的起 ...
- vue子传父、父传子
子传父 vue子传父使用$emit传值 子组件: <template> <div> <button @click="toParent">点击传到 ...
- VR和AR
VR 虚拟现实,比如你天气很炎热,戴上VR眼镜,出现在你眼前的是哈尔滨的冰雪世界 AR 增强现实,比如你站在一片碧绿的草地上,给你身边来一匹快马 想想一个单身狗去电影院,给你身边来一个美女作陪,这是多 ...
- php array_splice()函数 语法
php array_splice()函数 语法 作用:从数组中移除选定的元素,并用新元素取代它.dd马达价格 语法:array_splice(array,start,length,array) 参数: ...
- 使用WebAPI流式传输大文件(在IIS上大于2GB)
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- 数据导入导出mysql版本不同导致的问题
5.6.16-log导出.5.5.47-log导入 `addtime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '记录生产时间', 需要修改为: `add ...
- 【转】Django-template模板语言
Django-template模板语言 转自:https://www.cnblogs.com/zzy-9318/p/8672945.html 一.常用语法 只需要记两种特殊符号: {{ }}和 {% ...
- 关于Spring中BeanUtils的一次使用问题记录
1.问题描述:今天在进行前后端联调的时候,发现商品图片不能正常显示: 2.排查过程:查看浏览器控制台,发现调用接口返回的数据关于图片的字段未返回数据: 然后,又跑了一下Dao层的单元测试,从 ...