要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串。)

何为回文字符串? A palindrome is a string which reads the same in both directions. For example, “aba” is a palindome, “abc” is not.

解决方法:参考:http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

1.暴力法Brute force solution

共 C(N, 2) 个子串。时间复杂度O(N3)

2.后缀树法Suffix tree solution

反转 S 得到 S' ,例如:S = “caba”, S’ = “abac”. 求最长公共子串。求最大公共子串方法有后缀树动态规划方法。

可参考:http://en.wikipedia.org/wiki/Longest_common_substring

注意陷阱:例如,S = “abacdfgdcaba”, S’ = “abacdgfdcaba”. 最长公共子串是 “abacd” ,明显不是回文串。

陷阱原因:S 中有它的非回文子串的反转出现。

克服方法:对于找到的公共子串,查找它在两个母串的全部下标是否都相等。

3.动态规划法Dynamic programming solution

时间复杂度 O(N2), 空间复杂度  O(N2)。

定义数组:

则有,

dp[i][j] = d[i+1][j-1] && S[i] == S[j]

初始条件:

dp[i][i] = ture , i = 1,……,s.length() -1.

dp[i][i+1] =  (S[i] == S[i+1]) , i = 1,……,s.length()-2

代码:

class Solution {
public:
string longestPalindrome(string s) {
/************** 动态规划 *************/
int n = s.length();
if(n == 0) return "";
bool dp[1000][1000] = {false};
int firstIndex = 0, maxLen = 1;
for(int r = 0; r <= n - 1; ++r) {
for(int index = 0; index <= n - 1 - r; ++index) {
if(s[index] == s[index + r]){
if(r < 2){
dp[index][index + r] = true;
firstIndex = index;
maxLen = r + 1;
}else if(dp[index + 1][index + r -1] == true){
dp[index][index + r] = true;
firstIndex = index;
maxLen = r + 1;
}
}
}
}
return s.substr(firstIndex, maxLen);
}
};

 4.更简单的方法

O(N2)时间,O(1)空间

分析:回文串一定有一个中心,然后关于中心对称。长度为 N 的字符串有潜在的对称中心数目为 N + (N - 1) = 2N -1。

基于每个中心去找时间复杂度为 O(N),共查找 2N-1 次。因此总的时间复杂度为 O(N(2N - 1)) ~ O(N2).

代码:

string expandAroundCenter(string s, int left, int right){
int n = s.length();
while(left >= 0 && right <= n - 1 && s[left] == s[right]){
--left;
++right;
}
return s.substr(left + 1, right - left - 1);
} class Solution {
public:
string longestPalindrome(string s) {
/************** O(n^2)time, O(1)space complexity *************/
int n = s.length();
if(n == 0) return "";
string longest = s.substr(0,1);
for(int index = 0; index < n; ++index){
string s1 = expandAroundCenter(s, index, index);
string s2 = expandAroundCenter(s, index, index + 1);
if(s1.length() > s2.length()){
if(s1.length() > longest.length()) longest = s1;
}else if(s2.length() > longest.length()) longest = s2;
}
return longest;
}
};

5.Manacher’s Algorithm

时间复杂度 O(N),空间复杂度 O(4N)

算法思路:第一步:对于输入的长度为 N 的字符串 S,在字符间的 N+1 个位置分别插入一个特殊符号 "#",得到新的字符串 T ,长度为 2N+1。

例如:S = “abaaba”, T = “#a#b#a#a#b#a#”.

第二步:定义数组 P[2N+1]。分别以 T 中每个字符为回文串的中心,查找它的半径的值,结果存放在 P 中。(注意:数组 P 中最大数等于 S 中最大回文子串的长度)

例如:

 (则 S 的最大回文子串长度为 6)。

(注意:P 中数字关于中心对称)

接下来,主要就是对第二步的分析和优化。

以下面比较复杂的字符串为例:(可以看英文解释,也可以看我的叙述)

变量解释:L 和 R 分别为以 C 为中心的回文串的左右边界,其中 L + R = 2*C。分析两种情况:

index13  关于 C 对称点为 index(2*c-13) ,即 index9 ,因为 P[9] = 1,而且 P[9] < R-12(小于左边界) ,所以 P[13] = P[9] = 1;

……

index15  关于 C 对称点为 index(2*c-15) ,即 index7 ,因为 P[7] = 7,而且 P[7] >= R-15(大于左边界) ,所以 :

首先令P[15] = P[7] = 7,然后令 C =  15,以 index15 为中心,以 T[15-P[15]-1] 和 T[15+P[15]+1] 开始比较,向两段延伸,找新的 L 和 R.

最后得到 P[15] =  13 ,L = 2,R = 8。

……

代码:(为了方便边界,两边加了的特殊符号)

string preprocess(string s){
int n = s.length();
if(n == 0) return "";
string newS = "^#";
for(int i = 0; i < n; ++i)
newS += "#" + s.substr(i,1);
newS += "#$";
return newS;
} class Solution {
public:
string longestPalindrome(string s) {
/************** O(n)time, O(4n)space complexity *************/
string T = preprocess(s);
int n = T.length();
if(n == 0) return "";
int *p = new int[n];
int C = 0, R = 0;
int maxLen = 1, firstIndex = 0;
for(int i = 1; i < n-2; ++i){
int i_mirror = 2 * C - i;
p[i] = (R > i) ? min(R - i, p[i_mirror]) : 0;
while(T[i + p[i] + 1] == T[i - p[i] - 1]) {
++p[i];
if(p[i] > maxLen){
maxLen = p[i];
firstIndex = (i - 1 - p[i]) / 2;
}
}
if(i + p[i] > R){
C = i;
R = i + p[i];
}
}
return s.substr(firstIndex, maxLen);
}
};

1. Longest Palindromic Substring ( 最长回文子串 )的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

  4. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  5. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  7. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  8. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  9. LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...

随机推荐

  1. launch文件

    launch在ROS应用中,每个节点通常有许多参数需要设置,为了方便高效操作多个节点,可以编写launch文件,然后用roslaunch命令运行roslaunch: roslaunch [option ...

  2. C++ / CLI 调用 C++ /Native 随记

    C# 封装 原生C++ 方法:1.C++ CLR(托管)  调用 C++(原生)2.C#调用C++ CLR ,  注意各个平台编译版本需一致.3.C# 默认编绎生成版本是 any cpu , 需修改成 ...

  3. Appium移动自动化测试(一)--安装Appium

    Appium 自动化测试是很早之前就想学习和研究的技术了,可是一直抽不出一块完整的时间来做这件事儿.现在终于有了. 反观各种互联网的招聘移动测试成了主流,如果再不去学习移动自动化测试技术将会被淘汰. ...

  4. Sql获取第一天、最后一天

    昨天面试一家公司,上机题目中要求获取每月最后一笔订单.用到了日期的选择性查询,回来在ITeye上找到了这篇文章. 原文: http://new-fighter.iteye.com/blog/17587 ...

  5. docker 源码分析 一(基于1.8.2版本),docker daemon启动过程;

    最近在研究golang,也学习一下比较火的开源项目docker的源代码,国内比较出名的docker源码分析是孙宏亮大牛写的一系列文章,但是基于的docker版本有点老:索性自己就git 了一下最新的代 ...

  6. jQuery里ajax的用法

    $.ajax({ type:'post',//这里页面数据发送请求的方式可以为post和get cache:'false ', //这里可以为false或者true 是否要缓存 ,默认为false u ...

  7. IOS 瀑布流

    本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...

  8. How to implement updatable view with NHibernate

    see : http://msdn.microsoft.com/en-us/library/ms187956.aspx(The constrains of creation updatable vie ...

  9. 设置ASP.NET页面的运行超时时间详细到单个页面及站点

    这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下     全局超时时间 服务器上如果有多个网站,希望统一设置一 ...

  10. oracle 存储过程的写法

    create or replace procedure Getyc is  v_id VARCHAR2(36);  v_date VARCHAR2(4); begin  declare    begi ...