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 ...
随机推荐
- C内存管理
一般而言,分配给进程的内存有四个概念上不同的区域,分别为:代码段.数据段.堆和栈,其中数据段又可以细分为初始化为非零的数据和初始化为零的数据.如下图所示: 1.栈区(stack)— 由编译器自动分配释 ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- Linux 线程优先级
http://www.cnblogs.com/imapla/p/4234258.html http://blog.csdn.net/lanseshenhua/article/details/55247 ...
- Oracle并行更新的两种方式(merge/update内联视图)
对于Oracle的两表联合更新的场景(有A.B两表,以A.id=B.id关联,根据B表中的记录更新A表中的相应字段),一般有update内联视图和merge两种方式,下面举例介绍: 创建用例表: ...
- WPF——传实体类及绑定实体类属性
public class User: private string _User; public string User1 { get { return _User; } set { _User = v ...
- bzoj3503
显然知道第一行就可以只道整个矩阵但n<=40,搜索是不行的,我们设第一行为x1~xm可以由轻易由第一行未知数推出第n+1行,这一步我们可以压成二进制位(因为只和奇偶有关)显然n+1行必须是0,由 ...
- C#如何获取真实IP地址
大家获取用户IP地址常用的方法是 C# 代码 复制 string IpAddress = ""; if((HttpContext.Current.Request.Serve ...
- cogs_396_魔术球问题_(最小路径覆盖+二分图匹配,网络流24题#4)
描述 http://cojs.tk/cogs/problem/problem.php?pid=396 连续从1开始编号的球,按照顺寻一个个放在n个柱子上,\(i\)放在\(j\)上面的必要条件是\(i ...
- [HDU 1561] The more, The Better
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- spring(6)--注解式控制器
6.1.注解式控制器简介 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.已经@Deprecated. 二.Spring2.5引入注解式处理器支持 ...