【模板】Manacher 回文串

推荐两个讲得很好的博客:
http://blog.sina.com.cn/s/blog_70811e1a01014esn.html
https://segmentfault.com/a/1190000003914228
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N (22000010)
using namespace std;
int n, ans, mid, mx;
int p[N];
char tmp[N],s[N];
inline int max(int a, int b){return a>b?a:b;}
int main(){
scanf("%s", tmp+); n=strlen(tmp+);
for(int i=;i<=n;i++) s[i<<]=tmp[i], s[i<<|]='#';
n=(n<<)+;
s[]=s[n]='#';
ans=mid=mx=;
for(int i=;i<=n;i++){
if(mx>i) p[i]=min(p[(mid<<)-i],mx-i);
else p[i]=;
while(s[i-p[i]]==s[i+p[i]]) p[i]++;
if(p[i]+i>mx) mx=p[i]+i, mid=i;
ans=max(ans, p[i]);
}
return printf("%d\n", ans-),;
}
洛谷 3805
【模板】Manacher 回文串的更多相关文章
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- UVA 12378 Ball Blasting Game 【Manacher回文串】
Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- Best Reward---hdu3613(manacher 回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是 ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- 最长回文---hdu3068 (回文串 manacher 算法模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意很清楚:就是求一个串s的子串中最长回文串的长度:这类题用到了manacher算法 #incl ...
- Manacher模板(O(n)内求最长回文串长度)
转自:https://segmentfault.com/a/1190000008484167 /* 由于回文分为偶回文(比如 bccb)和奇回文(比如 bcacb),而在处理奇偶问题上会比较繁琐,所以 ...
- 回文串--Manacher算法(模板)
用途:在O(n)时间内,求出以每一个点为中心的回文串长度. 首先,有一个非常巧妙的转化.由于回文串长度有可能为奇数也有可能为偶数,说明回文中心不一定在一个字符上.所以要将字符串做如下处理:在每两个字母 ...
- POJ 3974 回文串-Manacher
题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...
随机推荐
- MongoDB使用初步
我很快就要离开现在这个使用nodejs + mongodb + redis的项目,转而去搞 塞特ID 之类的别的项目了.可惜这些技术对我来说浅尝辄止,半生不熟,胎死腹中.业余时间自学当然也可以,但哪有 ...
- LCA__st算法&&树上倍增
st表 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ]; ] ...
- Vue 页面回退参数被当作字符串处理
当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...
- C# 利用反射进行类型转换
/// <summary> /// 父类转子类 /// </summary> /// <typeparam name="TParent">< ...
- 2 我们的C#学习方法
在这里我们借鉴了一种行之有效的学习编程语言的方法,并在此基础上进行了相应的改良.我们在培训新人中使用后,发现这种学习方法是非常有效的. 你通过做以下几个步骤来一步步学习C#语言. 1. 搞懂每一个我们 ...
- (转)容易遗忘的JS知识点整理
1.hasOwnProperty相关 为了判断一个对象是否包含自定义属性而不是原型链上的属性,我们需要使用继承自 Object.prototype 的 hasOwnProperty方法.hasOwnP ...
- Ubuntu安装gnome-shell桌面环境
1.sudo apt-get install gnome-shell 输入命令直接回车就行了 2.出现了这个问题 Unable to locate package ?? sudo apt-get up ...
- HashTable, HashSet, HashMap的区别
HashTable, HashSet, HashMap的区别 hash是一种很常见也很重要的数据结构,是用hash函数根据键值(key)计算出存储地址,以便直接访问.由完美hash函数(即键值 ...
- 【译】x86程序员手册10 - 第4章系统架构
1.1.2 Part II -- Systems Programming 系统编程 This part presents those aspects of the architecture that ...
- Ubuntu用户自定义脚本开机启动
如果想让自己写的脚本随开机自动执行,可以这样做: 编辑文件/etc/init.d/rc.local,在最后添加用户自定义脚本的完整路径即可. 很简单有木有!!