首页 > 代码库 > 5. Longest Palindromic Substring

5. Longest Palindromic Substring

最长回文串

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

 

Example:

Input: "cbbd"

Output: "bb"

1.时间复杂度为O(N2)的算法-从中间向两边展开

回文字符串显然有个特征是沿着中心那个字符轴对称。比如aha沿着中间的h轴对称,a沿着中间的a轴对称。那么aa呢?沿着中间的空字符‘‘轴对称。 所以对于长度为奇数的回文字符串,它沿着中心字符轴对称,对于长度为偶数的回文字符串,它沿着中心的空字符轴对称。 对于长度为N的候选字符串,我们需要在每一个可能的中心点进行检测以判断是否构成回文字符串,这样的中心点一共有2N-1个(2N-1=N-1 + N)。 检测的具体办法是,从中心开始向两端展开,观察两端的字符是否相同。代码如下:

//从中间向两边展开  
string expandAroundCenter(string s, int c1, int c2) {  
  int l = c1, r = c2;  
  int n = s.length();  
  while (l >= 0 && r <= n-1 && s[l] == s[r]) {  
    l--;  
    r++;  
  }  
  return s.substr(l+1, r-l-1);  
}  
   
string longestPalindromeSimple(string s) {  
  int n = s.length();  
  if (n == 0) return "";  
  string longest = s.substr(0, 1);  // a single char itself is a palindrome  
  for (int i = 0; i < n-1; i++) {  
    string p1 = expandAroundCenter(s, i, i); //长度为奇数的候选回文字符串  
    if (p1.length() > longest.length())  
      longest = p1;  
   
    string p2 = expandAroundCenter(s, i, i+1);//长度为偶数的候选回文字符串  
    if (p2.length() > longest.length())  
      longest = p2;  
  }  
  return longest;  
}  

2.Manacher算法:求解最长回文字符串,时间复杂度为O(N)

参考:https://segmentfault.com/a/1190000003914228

string longestPalindrome(string s) {
        string t ="$#";
        for (int i = 0; i < s.size(); ++i) {
            t += s[i];
            t += #;
        }
        int p[t.size()] = {0}, id = 0, mx = 0, resId = 0, resMx = 0;
        for (int i = 0; i < t.size(); ++i) {
            p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
            while (t[i + p[i]] == t[i - p[i]]) ++p[i];
            if (mx < i + p[i]) {
                mx = i + p[i];
                id = i;
            }
            if (resMx < p[i]) {
                resMx = p[i];
                resId = i;
            }
        }
        return s.substr((resId - resMx) / 2, resMx - 1);
    }

同时还有 http://blog.csdn.net/yzl_rex/article/details/7908259

            http://www.cnblogs.com/grandyang/p/4464476.html

5. Longest Palindromic Substring