[LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
方法:
Manacher's algorithm,具体看这里http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
或者有比较好的博客:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
编码步骤如下:
1)在s字符串字符间添加特殊字符,使得原s字符串无论是奇数长度还是偶数长度,都变为奇数长度处理,变化后的字符串记为sNew;
2)用数组P[id]记录以字符sNew[id]为中心的最长回文串的半长度;
前两步如下所示:
s : w a a b w
sNew: $ # w # a # a # b # w #
P[id] : 1 1 2 1 2 3 2 1 2 1 2 1
3) 在O(n)时间求出数组P,然后遍历一遍P,可以由sNew中恢复出最长回文串。
用 O(n)时间求出数组P的方法,见代码,里面有详细注释。
class Solution {
public:
string longestPalindrome(string s) {
string result;
string sNew(,'$');
sNew.push_back('#');
int len = s.size();
if(len==)
return result;
else if(len==)
return s;
for(int i=;i<len;i++){
sNew.push_back(s[i]);
sNew.push_back('#');
}//end for
int lenNew = *(len+);
vector<int> P(lenNew,);
Pk(P,lenNew,sNew);
vector<int> P0(P);
sort(P0.begin(),P0.end());
int maxlen = P0[lenNew-];
int index;
for( index=;index<lenNew;index++){
if(P[index]==maxlen)
break;
}
if(sNew[index]!='#'){
result.push_back(sNew[index]);
maxlen -= ;
index += ;
}else{
maxlen -= ;
index += ;
}
while(maxlen>){
result.insert(result.begin(),sNew[index]);
result.push_back(sNew[index]);
index += ;
maxlen -= ;
}
return result;
}
private:
void Pk(vector<int> &P,int n,string s){
int i,mx=,id;//id是对称中心点的下标,mx是对称段的上届
for(i=;i<n;i++){
if(mx > i)
P[i] = min(P[*id-i],mx-i);//j=2*id-i,j是i关于id的对称点(和底下的while语句合起来,使得P[i]不用多重复计算)
else
P[i]=;
while(s[i+P[i]]==s[i-P[i]])//计算以i点为中心的最大回文段,将最大回文长度的一半大小记录在P[i]中
P[i]++;
if(P[i]+i>mx){
mx=P[i]+i;//更新当前点的对称段上届mx
id = i;//更新当前对称中心点的下标id
}
}//end for
}//end func
};
[LeetCode] Longest Palindromic Substring(manacher algorithm)的更多相关文章
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 21.Longest Palindromic Substring(最长回文子串)
Level: Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- POJ1797 Heavy Transportation(SPFA)
题目要求1到n点的最大容量的增广路. 听说是最短路求的,然后乱搞就A了.. 大概能从Bellman-Ford的思想,dk[u]表示从源点出发经过最多k条边到达u点的最短路,上理解正确性. #inclu ...
- C# 序列化(Serialize)与反序列化(Deserialize)ZZ
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种种串行化的方式:1. ...
- 胜利大逃亡[HDU1253]
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- python 代码片段5
#coding=utf-8 # python 有两个主要数据类型:int和float.根据Kiss原则,python只有一宗整数类型int. print 3**3 print int('123') p ...
- 【BZOJ】2002: [Hnoi2010]Bounce 弹飞绵羊(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 (BZOJ挂了,还没在BZOJ测,先是在wikioi测过了,,) 囧.在军训时立志要学lct! ...
- 我装GitHub的过程
GitHub是老师推荐的没正真的使用过,这次安装也是按提示的,不知对否,且还没使用,只是记录一下自己的过程.我是在线安装的. 1.下载GitHub安装问价,双击开始安装 2.出现的可能是系统相关配置吧 ...
- MySQL的show语句大全
常用的MySQL show 语句列举如下: 1.show databases ; // 显示mysql中所有数据库的名称 2.show tables [from database_name]; // ...
- 那些年我们没能bypass的xss filter
个人很喜欢收集xss payload.在这里把自己平时挖xss时会用到的payloads列出来和大家一起分享.很希望大家能把自己的一些payload也分享出来.(由于 我是linux党,所以本文出现在 ...
- ubuntu 14.04安装
最近想搞spark.没有集群环境,想先在单机上跑.但是两年前一直用到现在的virtualbox已经太老,很多源都停更了.所以准备装ubuntu14.04. 0. 更新源.163的源有问题,用的是soh ...
- mysql没有delete操作,那是delete from操作,
1.mysql没有delete操作,那是delete from操作, 2.DELETE FROM table_name [WHERE Clause]