Palindrome(Manacher)
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 6183 | Accepted: 2270 |
Description
A string is said to be a palindrome if it reads the same both
forwards and backwards, for example "madam" is a palindrome while "acm"
is not.
The students recognized that this is a classical problem but
couldn't come up with a solution better than iterating over all
substrings and checking whether they are palindrome or not, obviously
this algorithm is not efficient at all, after a while Andy raised his
hand and said "Okay, I've a better algorithm" and before he starts to
explain his idea he stopped for a moment and then said "Well, I've an
even better algorithm!".
If you think you know Andy's final solution then prove it! Given a
string of at most 1000000 characters find and print the length of the
largest palindrome inside this string.
Input
program will be tested on at most 30 test cases, each test case is
given as a string of at most 1000000 lowercase characters on a line by
itself. The input is terminated by a line that starts with the string
"END" (quotes for clarity).
Output
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
manacher;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=;
char str[MAXN],s[MAXN<<];
int p[MAXN<<];
int Manacher(char *s,int len){
p[]=p[]=;
int ans=,id=,mx=;
for(int i=;i<len;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])p[i]++;
if(p[i]+i>mx)mx=p[i]+i,id=i;
ans=max(ans,p[i]-);
}
return ans;
}
int main(){
int flot=;
while(~scanf("%s",str),strcmp(str,"END")){
int len=strlen(str);
s[]='@';
for(int i=;i<len;i++){
s[*i+]='#';
s[*i+]=str[i];
}
s[*len+]='#';
printf("Case %d: %d\n",++flot,Manacher(s,*len+));
}
return ;
}
Palindrome(Manacher)的更多相关文章
- URAL 1297 Palindrome(Manacher)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...
- 【学习笔记】字符串—马拉车(Manacher)
[学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...
- O(n)回文子串(Manacher)算法
O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...
- HDU 4513 吉哥系列故事——完美队形II(Manacher)
Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...
- 牛客小白月赛13 小A的回文串(Manacher)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...
- 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...
- Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...
- POJ3974:Palindrome(Manacher模板)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 14021 Accepted: 5374 题目链接 ...
- Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...
随机推荐
- IO库 8.1
题目:编写函数,接受一个istream&参数,返回值类型也是istream&.此函数须从给定流中读取数据,直到遇到文件结束标志时停止.它将读取的数据打印在标准输出上.完成这些操作后,在 ...
- BZOJ 2946: [Poi2000]公共串( 后缀自动机 )
一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...
- phpUnit 断言
转自http://blog.163.com/qianxue126@126/blog/static/162723138201119113131125/ 布尔类型assertTrue 断言为真asse ...
- MYSQL alter procedure alter function 它们只可以更改过程的特性,不可以更改过程的逻辑。
例子: delimiter // create procedure proc_a(in numberA int) 这样create procedure 是正确的 begin select number ...
- Http record java
http://httpunit.sourceforge.net/doc/servletunit-intro.html https://code.google.com/p/http-impersonat ...
- BASE64URL解析
BASE64URL是一种在BASE64的基础上编码形成新的加密方式,为了编码能在网络中安全顺畅传输,需要对BASE64进行的编码,特别是互联网中. BASE64URL编码的流程: .明文使用BASE6 ...
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...
- Linux特殊权限:SUID、SGID、SBIT
SUID: 只对二进制程序有效 执行者对于程序需要有x权限 在程序运行过程中,执行者拥有程序拥有者的权限 例如: 普通用户执行passwd命令. ...
- 关于cvScalar的那些事
CvScalar 可存放在1-,2-,3-,4-TUPLE类型的捆绑数据的容器 该函数包含4个浮点成员,可以用来表示B(Blue),G(Green),R(Red),Alpha(表示图像的透明度) ty ...
- tar.xz文件怎样解压
XZ压缩最新压缩率之王 xz这个压缩可能非常多都非常陌生,只是您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直非常少,所以差点儿没有什么提起. 我是在下载phpmyadmin的 ...