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 ...
随机推荐
- [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
笔者将在windows下的eclipse写的代码拷贝到MAC下,发现中文会出现乱码. 最初笔者遇到这个问题的时候,在网络上寻找了解决办法,出来的第一个网页(http://blog.csdn.net/w ...
- OpenGL学习之windows下安装opengl的glut库
OpenGL学习之windows下安装opengl的glut库 GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows环境下的GLUT下载地址:(大小约为15 ...
- JavaScript版排序算法
JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...
- 在ProgressBar上加文字----显示百分比的进度条
http://www.cnblogs.com/3dant/archive/2011/04/25/2026776.html
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- js传参java接收乱码解决方案
js传参处理 encodeURI(encodeURI(name)); java接收处理 URLDecoder.decode(request.getParameter("name") ...
- The c programming language第一章节所有程序的实现
//打印第一个程序hello, word #include<stdio.h> int main() { printf("hello, world\n"); ; } // ...
- sqlserver、mysql、oracle各自的默认端口号
sqlserver默认端口号为:1433 URL:"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbname" D ...
- hive原生和复合类型的数据载入和使用
原生类型 原生类型包含TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY (Hive 0.8.0以上才可用),TIMESTAM ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...