[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 ...
随机推荐
- 游戏 slider
using UnityEngine; using System.Collections; public class La : MonoBehaviour { float verticalValue=0 ...
- BZOJ3833 : [Poi2014]Solar lamps
首先旋转坐标系,将范围表示成矩形或者射线 如果范围是一条线,则将灯按y坐标排序,y坐标相同的按x坐标排序, 对于y相同的灯,f[i]=min(i,它前面灯发光时刻的第k[i]小值), 线段树维护,$O ...
- CSS光标属性一览表
光标类型 CSS 把你的光标放到相应文字上查看效果 要注意光标的实际效果依赖于用户的系统设置,与你在这里看到的效果并不一定一致. 十字准心 cursor: crosshair; 手 cursor: p ...
- 【C语言】14-返回指针的函数与指向函数的指针
前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...
- Oracle用户信息查询
1.查看所有用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角 ...
- hdu1028 Ignatius and the Princess III
这是道典型的母函数的题目,可以看看我的母函数这一标签上的另一道例题,里面对母函数做了较为详细的总结.这题仅贴上代码: #include"iostream" using namesp ...
- Css - Table.css
基本的Table样式 .gridtable { border: solid #ccc 1px; -moz-border-radius: 6px; -webkit-border-radius: 6px; ...
- PHP文件漏桐可以通过对服务器进行设置和配置来达到防范目的
对脚本执行漏洞的防范 黑客利用脚本执行漏洞进行攻击的手段是多种多样的,而且是灵活多变的,对此,必须要采用多种防范方法综合的手段,才能有效防止黑客对脚本执行漏洞进行攻击.这里常用的方法方法有以下四种.一 ...
- 移动Web应用开发入门指南——兼容篇
兼容篇 兼容篇是我最想写的一部分,在这之前也总结过很多关于移动开发的兼容问题与解决方案.对于移动Web开发来说,兼容是开发重心,通常要花费30%甚至更多的时间去处理一些兼容问题,甚至时间花掉了,问题依 ...
- tunnel.p4
Tunneling: VXLAN and NVGRE (including L2/L3 Gateway), Geneve, GRE and IPinIP /* Copyright 2013-prese ...