[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 ...
随机推荐
- BZOJ4327 : JSOI2012 玄武密码
对所有询问串建立AC自动机. 然后将母串在AC自动机上跑,每走到一个点x,从x点出发沿着fail指针能到的所有前缀都是匹配成功的,暴力向上走,碰到走过的就break,这样每个点最多只会被标记一次. 时 ...
- 【BZOJ】2802: [Poi2012]Warehouse Store(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=2802 自己yy了一下... 每一次如果够那么就买. 如果不够,考虑之前买过的,如果之前买过的比当前花 ...
- POJ 3373 Changing Digits(DP)
题目链接 记录路径的DP,看的别人的思路.自己写的也不好,时间居然2000+,中间的取余可以打个表,优化一下. 写的各种错,导致wa很多次,写了一下午,自己构造数据,终于发现了最后一个bug. dp[ ...
- hdu 最大报销额
本题也是一个背包的问题,我觉得这道题的核心就是根据精确度将浮点型转化为整型然后利用动态规划进行求解,注意对题意的理解,有3种支票是不能够报销的. 我开始照着这个思路进行思考,但是敲出来的第一个代码居然 ...
- Javascript - 数组去重复
这里我使用的场景是将表单中所有的input的值塞入数组中,然后通过去除重复的值.如果数组的长度和原数组的长度一致,说明没有重复,如果不一致(少于)则报错 //通过$.unique对数组进行“去重”,再 ...
- Java下利用Jackson进行JSON解析和序列化
Java下利用Jackson进行JSON解析和序列化 Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...
- 连连看的设计与实现——四人小组项目(NABCD)
小组名称:天天向上 成员:王森.张政,张金生,栾骄阳 题目:连连看游戏 NABCD N(需求) 游戏最大的乐趣在于玩法,我们要想在众多的连连看游戏当中脱颖而出,就需要增加更多富有乐趣.吸引用户的玩法. ...
- AMD GPU spec (public)
http://www.x.org/docs/AMD/old/ Index of /docs/AMD/old Name Last modified Size Description Parent Dir ...
- ThinkPHP 3.2.2 实现持久登录 ( 记住我 )
实现持久登录,即用户在登录时,勾选了"记住我"之后,无论是否关闭浏览器,只要不退出登录,在指定的时间内始终保持登录状态(缺点是在另一台电脑上登录过后,之前那台电脑就不能继续保持登录 ...
- 循环btn上面的视图
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...