确定当前已知能匹配到的最长处,看是否要更新最长

#include <bits/stdc++.h>
using namespace std;
const int N = 210005;
int p[N];
char str[N], s[N];
int main() {
while (~scanf("%s", str)) {
int n = strlen(str);
for (int i = 0; i<n; i++) {
s[2 * i] = '#';
s[2 * i + 1] = str[i];
}
s[2 * n] = '#';
s[2 * n + 1] = '\0';
n = 2 * n + 1;
int j = 0, mx = 0, id = 0;
for (int i = 0; i < n; i++)p[i] = 0;
for (int i = 0; i<n; i++) {
if (i > mx)mx = i, id = i;
j = id + id - i;
if (j>=0&&j<n&&j - p[j]<=id + id - mx) {
while (mx + 1 < n&&i + i - mx - 1 >= 0 && s[mx + 1] == s[i + i - mx - 1]) {
mx++;
p[i] = mx - i;
id = i;
}
}
else p[i] = p[j];
}
int ans = 0;
for (int i = 0; i<n; i++)
ans = max(ans, p[i]);
printf("%d\n", ans);
}
return 0;
}

manacher 最长回文子串的更多相关文章

  1. Manacher 最长回文子串。

    最长回文子串就是一个字符串的一个子串,他从左往右读和从右往左读是一样的. 可以用 Manacher 算法来求,他的复杂度是 O(n) . 可以看这篇文章 http://blog.csdn.net/yw ...

  2. POJ3974 Palindrome Manacher 最长回文子串模板

    这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...

  3. manacher最长回文子串

    https://www.luogu.org/blog/codesonic/manacheralgorithm 先放上洛谷的链接,毕竟讲的真好 两道例题 luogu4555 SP7586 inline ...

  4. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  5. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  6. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  7. 求最长回文子串:Manacher算法

    主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...

  8. Manacher's algorithm: 最长回文子串算法

    Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...

  9. 【转】最长回文子串的O(n)的Manacher算法

    Manacher算法 首先:大家都知道什么叫回文串吧,这个算法要解决的就是一个字符串中最长的回文子串有多长.这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文 ...

随机推荐

  1. getWindow().setFlags

    //设置窗体全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams ...

  2. Nginx 常用全局变量 及Rewrite规则详解

    每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...

  3. Bootstrap 模态框插件

    一.基本使用 使用模态框的弹窗组件需要三层 div 容器元素,分别为 modal(模态声明层). dialog(窗口声明层).content(内容层).在内容层里面,还有三层,分别为 header(头 ...

  4. https centos6 and 7

    keytool -printcert -sslserver 10.10.192.90:8443 -rfc >nexus.crt 通过  openssl 将 证书转换为 .pem格式的 通过以下命 ...

  5. iftop ifstat

    ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB/s in KB/s out KB/s in KB/s out 0 ...

  6. Delphi调用约定

    Register Calling Convention Ojbect Pascal的默认调用约定为register,寄存器调用约定会将前三个参数依次放入eax,edx,ecx,返回值是eax(根据类型 ...

  7. JQuery源码之“名叫extend的继承”

    提起JS中的继承很多”大神“们都会提起call,apply,单纯的对象赋值继承,以及原型链继承等众多的方式以及它们的不足之处,而且还会不时的把一些面向对象的设计模式”团团“的带出来,可谓是厉害非常啊! ...

  8. ubuntu下的jdk安装

    软件环境: 虚拟机:VMware Workstation 10 操作系统:ubuntu-12.04-desktop-amd64 JAVA版本:jdk-7u55-linux-x64 软件下载地址: JD ...

  9. 如何方便的控制css3动画开始时间点与持续时间

    一般我们在控制css3 animate动画时可以通过简写以减少代码量,只要在需要动画的元素上追加一下类名就可以了,如下例子 /*淡入并向上移动一点位置出现*/ .fadeInUp{ -webkit-a ...

  10. Dynamics AX 从数据库二进制数据导出图片

    // andy 2014/12/10 static void SSW_Bit2ImageFile(Args _args) { Bitmap curBitmap; Image curImage; ; c ...