[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 ...
随机推荐
- 基于Extjs的web表单设计器 第四节——控件拖放
接着上一节介绍控件拖放的设计. 通过前面的介绍知道,我们的区域类型的容器控件有三种:Card.Table.Mixed. Card 可以支持几乎所有的常用控件,包括:文本TextField.多文本Tex ...
- Machine Schedule
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- TYVJ P1082 找朋友 Label:字符串
描述 童年的我们,对各种事物充满了好奇与向往.这天,小朋友们对数字产生了兴趣,并且想和数字交朋友.可是,怎么分配这些数字才能使得每个小朋友都唯一地找到一个数字朋友呢?C小朋友说:咱们按自己名字的字典序 ...
- [Cocos2d-x For WP8]Action 常用动作
Action相当于是Cocos2d-x里面的动画操作,在Cocos2d-x里面的动画基类是CCAction类,从CCAction类派生出来的就有很多常用的动作的实现类,利用这些类就可以给我们游戏的精灵 ...
- POJ 1095 Trees Made to Order(卡特兰数列)
题目链接 中间计算的各种细节.有的细节没处理好,就wa了...主要思路就是根据卡特兰数列的: h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n&g ...
- oracle系列--第一篇 数据库基础
第一章 数据库基础 1.1 数据管理概述 1.1.1 什么是数据管理 与我们人类相比,计算机的最大优势就是能够高速.精准地运行,其运行的过程就是执行程序代码和操作指令.处理数据的过程.可以说,数据处理 ...
- FileUpload上传图片直接浏览显示(没有上传按钮如何上传)
1.给FileUpload添加一个onchange事件:FileUpload1.Attributes.Add("onchange", "document.getEleme ...
- asp.net 微信企业号办公系统-流程设计--保存与发布
如果流程未设计完时可以先保存,以后再打开接着设计.点击工具栏上的保存按钮即可保存当前流程设计: 如果下次要接着设计,则可以打开该流程继续设计: 如果流程设计完成,可以点击安装按钮来发布流程,流程安装成 ...
- jstl fn标签
JSTL使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请 求参数或者context以及session中的数据的访问非常方便,但是我们在实际应用中经常需要 ...
- 使用Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...