求最长回文子序列的 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 模板的更多相关文章

  1. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  2. HDU 3068 最长回文(manacher模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...

  3. HDU 3068 最长回文( Manacher模板题 )

    链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...

  4. HDU3068:最长回文(Manacher模板)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Manacher模板( 线性求最长回文子串 )

    模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...

  6. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  7. manacher模板(manacher)

    洛谷题目传送门 写完有一段时间了,发现板子忘记存在了这里...... 算法简述 一种字符串算法,\(O(n)\)高效求出以每个字符为对称中心的最长回文串长度. 然后,就可以进一步求出全串中最长回文串的 ...

  8. 最长回文(manacher模板)

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  9. 最长回文 HDU - 3068 manacher 模板题

    题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...

随机推荐

  1. android狼人杀源码,桌面源码,猎豹快切源码

    Android精选源码 android实现狼人杀app源码 android实现精心打造的Android基础框架源码 android热门电影的客户端源码 android 实现桌面的Launcher源码 ...

  2. linux下各文件夹的结构说明及用途介绍

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令.   /dev:设备特殊文件.   /etc:系统管理和配置文件.   /etc/rc.d:启动的配 置文件和脚本.   /ho ...

  3. SpringMVC框架学习笔记(5)——数据处理

    1.提交数据的处理 a)提交的域名称和参数名称一致 http://localhost:8080/foward/hello.do?name=zhangsan 处理方法 @RequestMapping(v ...

  4. UEP-查询方式总结

    public void retrieve() { QueryParamList params = getQueryParam("dataWrap"); //获取页面上的参数,即查询 ...

  5. ubuntu 下 apt /apt-get command not found 命令找不到

    简介:apt 命令在ubuntu下找不到.(针对云平台,等可联网的ubuntu  如果是虚拟机,请确认能否联网 (如是虚拟机且不能联网请参考其他文章,大致方向是先挂载系统镜像再安装)) (ps:一般的 ...

  6. .23-浅析webpack源码之事件流compilation(1)

    正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...

  7. Map,List,POJO深拷贝(序列化实现)方法与注意事项

    转载请注明出处,谢谢! 方法1: /*jdk >= 1.5*/ @SuppressWarnings("unchecked") public static <T> ...

  8. thinkphp 中concat(连接)使用方法

    1.concat将title和id连接作为truename新的字段,left从url字段左侧开始截取25个字符,同理right也可. 2.getLastql用法:

  9. Java数据结构和算法(十三)——哈希表

    Hash表也称散列表,也有直接译作哈希表,Hash表是一种根据关键字值(key - value)而直接进行访问的数据结构.它基于数组,通过把关键字映射到数组的某个下标来加快查找速度,但是又和数组.链表 ...

  10. ngRx 官方示例分析 - 1. 介绍

    ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book  API  来查询图书,并保存自己的精选书籍列表. 菜单有两 ...