manacher 最长回文子串
确定当前已知能匹配到的最长处,看是否要更新最长
#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 最长回文子串的更多相关文章
- Manacher 最长回文子串。
最长回文子串就是一个字符串的一个子串,他从左往右读和从右往左读是一样的. 可以用 Manacher 算法来求,他的复杂度是 O(n) . 可以看这篇文章 http://blog.csdn.net/yw ...
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- manacher最长回文子串
https://www.luogu.org/blog/codesonic/manacheralgorithm 先放上洛谷的链接,毕竟讲的真好 两道例题 luogu4555 SP7586 inline ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- 1089 最长回文子串 V2(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa ...
- 51nod1089(最长回文子串之manacher算法)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...
- 求最长回文子串:Manacher算法
主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...
- Manacher's algorithm: 最长回文子串算法
Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...
- 【转】最长回文子串的O(n)的Manacher算法
Manacher算法 首先:大家都知道什么叫回文串吧,这个算法要解决的就是一个字符串中最长的回文子串有多长.这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文 ...
随机推荐
- prepare PDO
<?php $sql='SELECT email FROM users WHERE id = :id'; $statement=$pdo->prepare($sql);$userId=fi ...
- P1541 乌龟棋
30分做法,暴力枚举: #include <bits/stdc++.h> using namespace std; const int maxn = 400; int n, m; int ...
- SQLQuery 实现别名映射
public List getQueryList(Map paramMap, int start, int maxResults) throws DataAccessException, Hibern ...
- foreach遍历 < 创建表 >练习题
原表如下: 效果图如下: <table border="1" width="500" height="260"><tr&g ...
- Delphi HOOK示例
本应用程序的Hook: unit UFrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- 变态的HelloWorld
public static void main(String[] args) { int i, n[] = { (((1 << 1) << (1 << 1) < ...
- C++中string转化为常用数值类型
//模板类 用于将string类型转化为 常用数值类型 template <class Type> Type stringToNum(const string& str) { is ...
- recordcount
rs.recordcount 有时不能取到数,这时 要更改游标为客户端游标 .
- .Net 文件名补位
文件以name-1.pdf.name-2.pdf.name-3.pdf......name-80.pdf命名,传到数据库中排序混乱:1之后为10,2之后是20,所以要把文件名中的数字补位变成相同位数, ...
- visual studio2013 添加 dll库
在visual studio2013中添加C#的dll库.记录如下: 在solution explorer处右键,选择reference--> add References 选择Browser. ...