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 ...
随机推荐
- Struts学习之模型驱动
* 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法. * 将javabean和业务方法进行分离: * 将重新创建一 ...
- ExtJS4.2学习(二)——入门基础
1.工程的目录结构: src里放后台的文件,WebRoot里放前台的文件. index.html或者index.jsp等是整个项目的首页,在首页我们要引入ExtJS的CSS样式文件和ExtJS的核心类 ...
- SQLite 字符串连接
对Mysql可以使用CONCAT进行字符串连接, 但使用sqlite时,没有找到相应的方法,后在网上查找后,可以使用||来连接字符串 例: select 'a'||'b'
- Android 中Java和JavaScript交互入门
如何实现JavaScript 和java 交互 实现Java和js交互十分便捷.通常只需要以下几步. WebView开启JavaScript脚本执行 WebView设置供JavaScript调用的交互 ...
- git创建分支与合并分支
git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...
- mysql 简单游标
<=====================MYSQL 游标示例=====================> CREATE PROCEDURE `test`.`new_procedure` ...
- unity3d 2d游戏制作的模式
经过了4个月不懈的努力,我和图灵教育合作的这本3D游戏开发书预计下个月就要出版了.这里MOMO先打一下广告,图灵的出版社编辑成员都非常给力,尤其是编辑小花为这本书付出了很大的努力,还有杨海玲老师, ...
- codeforces 540D 概率dp
传送门 大概可以这样理解, 一开始有r个石头, p个布, s个剪刀, 每一天有其中的两个相遇, 如果两个是相同的种类, 什么都不会发生, 否则的话有一个会挂掉, 问最后每一种生存的概率. dp[i][ ...
- 腾讯出品的抓包工具Rythem
Mac下一直没有fiddler这样好用的抓包工具,Charles要收费,难免不爽,昨天调研国内项目的时候,看到腾讯开源了一款抓包工具Rythem,试用了一下,基本配置无问题,但是通配符方面不太搞的定. ...
- 多版本jQuery的使用剖析
</div> </div> <!-- basic scripts --> <!--[if !IE]> --> <!-- <![endi ...