manacher 模板
求最长回文子序列的 O(n)做法
讲解
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN=11000117;
char s[MAXN],snew[MAXN*2+5];
int p[MAXN*2+5];
int init(){
int len=strlen(s);
snew[0]='&';snew[1]='#';
int j=2;
for(int i=0;i<len;i++){
snew[j++]=s[i];
snew[j++]='#';
}
snew[j]='\0';
return j;
}
int manacher(){
int ans=0;
int len=init();
int id=0,mx=0;
for(int i=1;i<len;i++){
if(i<mx) p[i]=min(p[id*2-i],mx-i);
else p[i]=1;
while(snew[i-p[i]]==snew[i+p[i]]) p[i]++;//注意这里的循环写在if外面,因为如果p[i]+i==mx,还可以向外扩展
if(i+p[i]>mx){
mx=i+p[i];
id=i;
}
ans=max(ans,p[i]-1);
}
return ans;
}
int main(){
freopen("in.txt","r",stdin);
scanf("%s",s);
printf("%d",manacher());
fclose(stdin);
return 0;
}
manacher 模板的更多相关文章
- ural 1297 Palindrome(Manacher模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...
- HDU 3068 最长回文(manacher模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...
- HDU 3068 最长回文( Manacher模板题 )
链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...
- HDU3068:最长回文(Manacher模板)
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Manacher模板( 线性求最长回文子串 )
模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- manacher模板(manacher)
洛谷题目传送门 写完有一段时间了,发现板子忘记存在了这里...... 算法简述 一种字符串算法,\(O(n)\)高效求出以每个字符为对称中心的最长回文串长度. 然后,就可以进一步求出全串中最长回文串的 ...
- 最长回文(manacher模板)
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- 最长回文 HDU - 3068 manacher 模板题
题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...
随机推荐
- 51nod1649- 齐头并进-最短路
1649 齐头并进 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 在一个叫奥斯汀的城市,有n个小镇(从1到n编号),这些小镇通过 ...
- 树状数组-HDU1541-Stars一维树状数组 POJ1195-Mobile phones-二维树状数组
树状数组,学长很早之前讲过,最近才重视起来,enmmmm... 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据 ...
- hdu_2604Queuing(快速幂矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others) ...
- const类型变量的详细解读
const类型变量--------------------------------------int i;const int *p; --------------------------------- ...
- C++ enum用法小技巧
enum DeviceDataType :int { None = 0, SourceRGBA32 = 1, Keying = ...
- 什么是WEBserver? 经常使用的WEBserver有哪些?
地址:http://www.mamicode.com/ 什么是WEBserver? 经常使用的WEBserver有哪些? 一.什么是WEBserver Webserver能够解析HTTP协议.当Web ...
- ecshop商城_
一.Ecshop简介: ECShop是Comsenz公司推出的一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店.系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序. ECSho ...
- 7系列高速收发器总结 GTP IP核使用篇
上一篇7系列收发器博文讲解了GTP IP核的基本配置,本文继续分析如何将它使用起来.生成IP核后打开example design,先看看工程中包含的文件结构. 顶层文件下包含了gtp ip核系统顶层文 ...
- Jupyter notebook入门
Jupyter notebook入门 [TOC] Jupyter notebook 是一种 Web 应用,能让用户将说明文本.数学方程.代码和可视化内容全部组合到一个易于共享的文档中. Jupyter ...
- centos7 下搭建hadoop2.9 分布式集群
首先说明,本文记录的是博主搭建的3节点的完全分布式hadoop集群的过程,环境是centos 7,1个nameNode,2个dataNode,如下: 1.首先,创建好3个Centos7的虚拟机,具体的 ...