[poj3974]Palindrome_Manacher
Palindrome poj-3974
题目大意:求字符串的最长回文子串。
注释:$1\le strlen(s) \le 10^6$.
想法:介绍一种字符串算法——Manacher。求以每一个字符和字符间隔为回文中心的回文半径长度。什么是Manacher?
我们先来考虑这样一种暴力:如果我们用暴力来达到Manacher的效果,我们需要枚举每一个字符以及字符间隔,然后分别向左右扩展更新当前答案,时间复杂度$O(n^2)$,极限数据:连续的同样字符。那么,我们如何对其进行优化?

我们显然不怎么会处理偶回文子串的方式,那么我们将每两个相邻字符之间加上'#',来达到只需要求出奇回文子串的效果(很巧妙)。
紧接着,上面的图表示:
id为已经处理过的字符串中回文子串最靠右的回文子串的回文中心。无论是字符还是'#'
mx是id的回文子串右端点。
更新... ...
int Manacher()
{
int maxLen=-1;
int mx=0;
int id=0;
for(int i=0;i<=n;i++)
{
if(i<mx)
p[i]=min(p[2*id-i],mx-i);
else p[i]=1;
while(s_new[i-p[i]]==s_new[i+p[i]]) p[i]++;//s_new是带'#'的新字符串
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
maxLen=max(maxLen,p[i]-1);
}
// for(int i=1;i<=n;i++)
// {
// cout << i << " " << s_new[i] << " " << p[i] << " " << endl;
// }
return maxLen;
}
显然,是正确的,然后以'#'为回文中心的回文子串就是偶数,反之为奇数。p[i]表示以s_new中的i为回文中心的回文子串的回文半径。
最后,附上丑陋的代码.. ....
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int p[2000100];
char s[1000100];
char s_new[2000010];
int Manacher()//Manacher
{
int maxLen=-1;
int mx=0;
int id=0;
for(int i=0;i<=n;i++)
{
if(i<mx)
p[i]=min(p[2*id-i],mx-i);
else p[i]=1;
while(s_new[i-p[i]]==s_new[i+p[i]]) p[i]++;//s_new是带'#'的新字符串
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
maxLen=max(maxLen,p[i]-1);
}
// for(int i=1;i<=n;i++)
// {
// cout << i << " " << s_new[i] << " " << p[i] << " " << endl;
// }
return maxLen;
}
void original()//初始化
{
memset(p,0,sizeof p);
n=0;
}
int main()
{
int count=0;
while(1)
{
original();
count++;
scanf("%s",s+1);
int k=strlen(s+1);
if(s[1]=='E') return 0;
printf("Case %d: ",count);
s_new[0]='$';//边界小技巧,不用特判
s_new[++n]='#';
for(int i=1;i<=k;i++)//建立新字符串
{
s_new[++n]=s[i];
s_new[++n]='#';
}
s_new[++n]='!';//+1
// for(int i=1;i<=k;i++) cout << s[i] ;
printf("%d\n",Manacher());
}
}
小结:Manacher好东西qwq
[poj3974]Palindrome_Manacher的更多相关文章
- 马拉车算法——poj3974
https://segmentfault.com/a/1190000008484167?tdsourcetag=s_pctim_aiomsg 讲的超好! manacher算法理解 回文串分为偶回文串和 ...
- POJ----(3974 )Palindrome [最长回文串]
Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 5121 Accepted: 1834 Description Andy ...
- POJ3974 (manacher)
var s,t:ansistring; n,op:longint; p:..] of longint; procedure pre; var i:longint; begin s:='$*'; to ...
- POJ3974 Palindrome (manacher算法)
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
- Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- poj3974(manacher)
传送门:Palindrome 题意:给定一个字符串,求最长回文子串. 分析:manach裸题,核心理解mx>i?p[i]=min(p[2*id-i],mx-i):1. #pragma comme ...
- Palindrome poj3974
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 3280 Accepted: 1188 Descr ...
- POJ--3974 Palindrome(回文串,hash)
链接:点击这里 #include<iostream> #include<algorithm> #include<stdio.h> #include<cstri ...
随机推荐
- E20171014-hm
Sibling n. 兄弟,姐妹; [生] 同科,同属; [人] 氏族成员;
- jsp简单学习总结
以下均为jsp页面 1:<jsp:include page="index.jsp"/>相当于嵌入一个页面.还有一种是<frame src="main_l ...
- Another lottery
http://acm.hdu.edu.cn/showproblem.php?pid=2985 题意:有n个人每个人可以买m轮彩票,每轮可以买尽可能多的彩票.如果该彩票在i轮被抽到,则该人可以获得2^i ...
- thinkphp的model的where条件的两种形式
thinkphp的model的where查询时有两种形式. $model->field('id')->where('customer_num is null or customer_num ...
- [Swift通天遁地]六、智能布局-(2)视图对象的尺寸和位置相对约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- java中使用String的replace方法替换html模板保存文件
在我们的D盘下有这样一个html模板,现在我们要做的就是解析news.template文件,从数据库中提取数据将数据添加到指定的模板位置上 <head> <title>{tit ...
- 《Java编程的逻辑》第一部分 编程基础与二进制
- Jupyter(Ipython) Notebook 入门
upyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 一般用来编写漂亮的交互式文档. 文学编程的读者不是机器,而是人. 我们 ...
- JS高级——作用域链
基本概念 1.只要是函数就可以创造作用域 2.函数中又可以再创建函数 3.函数内部的作用域可以访问函数外部的作用域 4.如果有多个函数嵌套,那么就会构成一个链式访问结构,这就是作用域链 <scr ...
- Oracle、Db2、SqlServer、MySQL 数据库插入当前系统时间
做易买网项目,由于对数据库插入系统时间不了解,常常遇到的问题: 1.java.sql.SQLException: ORA-01861: 文字与格式字符串不匹配.原因:由于获取系统时间类型不对,应为sy ...