要求: 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. bzoj 2761: [JLOI2011]不重复数字

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #d ...

  2. iOS 模拟器上录制Demo视频

    在App审核中会让用户上传一段简易的视频,那么如何制作改视频呢? 今天无意中在Cocochina 中看到 说利用QuickTime来制作,而QuickTime是操作系统自带的. 哦 My,God ! ...

  3. SQL分类

    SQL(Structure Query Language)结构化查询语言,是使用关系型数据库的应用语言. SQL主要可以划分为以下三个类别: DDL(Data Define Language)语句:数 ...

  4. JavaScript原型学习笔记

    1 理解JavaScript原型 什么是原型? 原型是一个对象,其他对象可以通过它实现属性继承. 任何一个对象都可以成为原型么? 是 哪些对象有原型 所有的对象在默认的情况下都有一个原型,因为原型本身 ...

  5. bxSlider 在网页里添加幻灯片效果

    幻灯片效果在网页上很常见,本文介绍用 bxSlider 轻松实现的方法. bxSlider是什么 bxSlider 是用 JQuery 和 CSS 实现网页中幻灯片效果的工具.可在 http://bx ...

  6. Sprint第二个冲刺(第十一天)

    看板: 燃尽图:

  7. uva674 Coin Change ——完全背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. 5X + 2Y +Z = 50 的所有非负整数解

    这种题的解题方法都差不多,不停的循环,不过如果做一下细分,效率应该可以提升很多,下面把最常规效率也最低的代码贴上,有时间再优化 #include <iostream> using name ...

  9. EDIUS设置Alpha转场的教程

    有刚开始学习EDIUS视频编辑软件的同学吗?你们是否需要一本很好的EDIUS教程呢?你们可以到EDIUS中文网站里面找哦,小编会一直更新EDIUS教程的,能给你们带来帮助我是非常高兴的.今天我们来一起 ...

  10. html5-websocket初探

    HTML5规范在传统的web交互基础上为我们带来了众多的新特性,随着web技术被广泛用于web APP的开发,这些新特性得以推广和使用,而websocket作为一种新的web通信技术具有巨大意义. 什 ...