Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974
 Palindrome
 Time Limit: 15000MSMemory Limit: 65536K
 Total Submissions: 2707Accepted: 995
 Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
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
Your 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
For each test case in the input print the test case number and the length of the largest palindrome. 
 Sample Input
abcbabcbabcba
 abacacbaaaab
 END
 Sample Output
Case 1: 13
 Case 2: 6
 Source
Seventh ACM Egyptian National Programming Contest
 解析:
 题意:求最长回文串长度:
 思路:
 利用manacher算法计算;
 这里简要概述一下这个算法;
 总的来说是相邻字符中间插入一个相同的字符构成一新的字符(以此可以避免奇偶性讨论。),然后求以每个点为中心的回文字长度。
 就这以下标号来解释一下:
 p[i]:以第i个字符为中心的回文长度;
 id:前一个回文中心.
 mx:前一个回文的最有右端 ,mx=p[id]+id;
 1.逐次枚举i判断是否在mx前面,更新p[i]初值;
 2.左右看扩展,求当前最大的p[i];
 3.更新  id,mx;
 4得出的结果为ans=max{p[i]-1};
 Accepted580K 47MSG++ 740B
 */
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<algorithm>
using namespace std;
const int maxn=1000000+10;
char s[maxn],st[maxn*2];
int p[maxn*2];
void manacher()
{
int i,j,len;
len=strlen(s);
for(i=0,j=0;i<len;i++,j+=2)//构造新数组
{
st[j]='#';
st[j+1]=s[i];
}
st[2*len]='#';//末尾处不可忽略
int mx=0,id;
for(i=1;i<=2*len;i++)
{
if(mx>i)//如果此时的中心点仍在前一个回文串中
{p[i]=min(p[2*id-i],mx-i);
}
else
p[i]=1;
//由中心向两边扩展
for(;st[i-p[i]]==st[i+p[i]]&&(i-p[i]>=0)&&(i+p[i]<=2*len);p[i]++) if(mx<i+p[i])//更新mx,id;
{
id=i;
mx=i+p[i];
}
}
int ans=0;
for(i=1;i<=len*2;i++)//得到最长回文字
{
if(p[i]>ans)
ans=p[i];
}
printf("%d\n",ans-1);
}
int main()
{ int ca=0;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"END")==0)
break;
printf("Case %d: ",++ca);
manacher();
}
return 0;
}
Palindrome(poj3974)(manacher算法)的更多相关文章
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
		
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
 - POJ 3974 Palindrome 字符串 Manacher算法
		
http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...
 - POJ3974 Palindrome (manacher算法)
		
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
 - 【Manacher算法】poj3974 Palindrome
		
Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: #include<cs ...
 - Palindrome(最长回文串manacher算法)O(n)
		
Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
 - 利用Manacher算法寻找字符串中的最长回文序列(palindrome)
		
寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...
 - hdu 3068 最长回文 manacher算法(视频)
		
感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...
 - HDU3068 最长回文 Manacher算法
		
Manacher算法是O(n)求最长回文子串的算法,其原理很多别的博客都有介绍,代码用的是clj模板里的,写的确实是异常的简洁,现在的我只能理解个大概,下面这个网址的介绍比较接近于这个模板,以后再好好 ...
 - 【转载】Manacher算法
		
本文原创:http://www.cnblogs.com/BigBallon/p/3816890.html只为了记录学习,不为抄袭!http://www.felix021.com/blog/read.p ...
 
随机推荐
- Asp.net MVC 视图之公用代码
			
一.公共模板 转自:http://www.cnblogs.com/kissdodog/archive/2013/01/07/2848881.html 1.@RenderBody() 在网站公用部分通过 ...
 - Codeforces Round #239 (Div. 2)   C. Triangle
			
time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard o ...
 - AVPicture、AVFrame和AVPacket
			
http://blog.csdn.net/ym012/article/details/6540065 从定义上可知,AVPicture是AVFrame的一个子集,他们都是数据流在编解过程中用来保存数据 ...
 - Delphi捕捉DLL执行所抛出的异常。
			
先来说一下我如何写我的Dll文件的. 先看代码: 代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://w ...
 - ThinkPHP3.2.3验证码显示、刷新、校验
			
显示验证码 首先在Home/Controller下创建一个公共控制器PublicController <?php namespace Home\Controller; use Think\Con ...
 - (转载)NET流操作
			
http://www.oseye.net/user/kevin/blog/86 概念 数据流(Stream)是对串行传输数据的一种抽象表示,是对输入/输出的一种抽象.数据有来源和目的地,衔接两者的就是 ...
 - 【HDOJ】2155 小黑的镇魂曲
			
线段树+SPFA最短路可以过.或者DP也能过.需要注意的是xl的范围是错的,测试用例中xl可能为0,他妈的,因为这个一直莫名其妙的wa.1. spfa建图增加一倍的点即可(讨论左端点和右端点). /* ...
 - bzoj1443
			
首先要思考的问题肯定是,什么点可以是ans, 不难想到对图黑白染色,假如一个棋子不管怎么走,都只能走到和它同色的点上时,这就是一个ans 再考虑,每次棋子的移动都是黑白相间的 再考虑,黑白染色是可以构 ...
 - POJ 1062 昂贵的聘礼 解题报告
			
本题不难,但是笔者贡献了30多次Submit……就像Discuss讨论的一样,细节决定成败,WA了肯定有理由. 贴代码,Dijkstra+优先队列. #include <cstdio> # ...
 - An error occurred while filtering resources
			
一个比较恶心的问题.在使用过程中项目中有个错误 ,找不到原因.. An error occurred while filtering resources Maven -> Update ...