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 ...
随机推荐
- java中获得jar包执行路径的方法
当我们由于某种需要需要的得到jar的路径是可以用下面的方式来获得: basePath = new Solution().getClass().getProtectionDomain().getCode ...
- 练习C之SELECT形式的非阻塞IO
呵呵,理解得不深,但毕竟手打全版,且无错.但select.h不知何处找头文件, 粘下来作个记录. POLL,EPOLL感觉代码类似,只是函数和系统实现不一样,,EPOLL目前最合理的.定位精确,算法复 ...
- 安装Ubuntu服务器
安装edX首先需要一台linux或Mac系统的电脑/服务器. 这里以常见的Ubuntu作为服务器系统. Ubuntu的官方网站为http://www.ubuntu.com,中文网站为http://ht ...
- strspn和strcspn妙用
http://blog.csdn.net/aidenliu/article/details/5460201
- Altium快捷键
M快捷键 PCB快捷键 编辑 视图
- 在redhat6.4下安装 Oracle® Database 11g Release 2
OS版本: 安装过程的相关信息: pdksh 安装好后根据需要设置oracle开机自启动http://www.cnblogs.com/softidea/p/3761671.html 设置环境变量NLS ...
- 安装gem invalid date format in specification错误的解决方法
别的不说,报错信息直接贴图: 解决方法: 1.找到你环境目录下的spec,例如:D:\Ruby187\lib\ruby\gems\1.8\specifications. 2.找到引起错误文件的gems ...
- PHP ‘asn1_time_to_time_t’函数内存损坏漏洞
漏洞名称: PHP ‘asn1_time_to_time_t’函数内存损坏漏洞 CNNVD编号: CNNVD-201312-348 发布时间: 2013-12-18 更新时间: 2013-12-18 ...
- Linux 的档案权限与目录配置
档案权限 Linux最优秀的地方之一,就在于他的多人多任务环境. 而为了让各个使用者具有较保密的档案数据,因此档案的权限 管理就变的很重要了. Linux一般将档案可存取的身份分为三个类别,分别是 o ...
- 使用IIS配合VS调试
当我们使用Visual Studio调试(Debug)的时候,通常我们会选择VS自带的ASP.NET Developerment Server(也是默认选项),当第一次调试的时候(按F5或Ctrl+F ...