最长回文子串

原创 2013年12月04日 20:22:25

方法一:时间O(N^2)    空间O(N^2)

// A dynamic programming solution for longest palindr.
// This code is adopted from following link
// http://www.leetcode.com/2011/11/longest-palindromic-substring-part-i.html
 
#include <stdio.h>
#include <string.h>
 
// A utility function to print a substring str[low..high]
void printSubStr( char* str, int low, int high )
{
    for( int i = low; i <= high; ++i )
        printf("%c", str[i]);
}
 
// This function prints the longest palindrome substring of str[].
// It also returns the length of the longest palindrome
int longestPalSubstr( char *str )
{
    const int n = strlen( str ); // get length of input string
 
    // table[i][j] will be false if substring str[i..j] is not palindrome.
    // Else table[i][j] will be true
    bool table[n][n];
    memset( table, 0, sizeof( table ) );
 
    // All substrings of length 1 are palindromes
    int maxLength = 1;
    for( int i = 0; i < n; ++i )
        table[i][i] = true;
 
    // check for sub-string of length 2.
    int start = 0;
    for( int i = 0; i < n-1; ++i )
    {
        if( str[i] == str[i+1] )
        {
            table[i][i+1] = true;
            start = i;
            maxLength = 2;
        }
    }
 
    // Check for lengths greater than 2. k is length of substring
    for( int k = 3; k <= n; ++k )
    {
        // Fix the starting index
        for( int i = 0; i < n - k + 1 ; ++i )
        {
            // Get the ending index of substring from starting index i and length k
            int j = i + k - 1;
 
            // checking for sub-string from ith index to jth index iff str[i+1]
            // to str[j-1] is a palindrome
            if( table[i+1][j-1] && str[i] == str[j] )
            {
                table[i][j] = true;
 
                if( k > maxLength )
                {
                    start = i;
                    maxLength = k;
                }
            }
        }
    }
 
    printf("Longest palindrome substring is: ");
    printSubStr( str, start, start + maxLength - 1 );
 
    return maxLength; // return length of LPS
}
 

写的简便些则如下:

string longestPalindrome(string &s)
{
    const int n = s.size();
    bool f[n][n];
    memset(f,0,sizeof(f));
    
    int max_len = 1,start = 0;
    
    for(int i = 0; i < n;++i)
    {
        f[i][i] = true;
        for(int j = 0; j < i;++j)
        {
            f[j][i] = (s[j]==s[i] && (i-j<2 || f[j+1][i-1]));
            if(f[j][i] && max_len < i-j+1)
            {
                max_len = i-j+1;
                start = j;
            }
            
        }
    }
    return s.substr(start,max_len);
}


方法二:时间O(N^2)   空间O(1)

// A O(n^2) time and O(1) space program to find the longest palindromic substring
#include <stdio.h>
#include <string.h>
 
// A utility function to print a substring str[low..high]
void printSubStr(char* str, int low, int high)
{
    for( int i = low; i <= high; ++i )
        printf("%c", str[i]);
}
 
// This function prints the longest palindrome substring (LPS)
// of str[]. It also returns the length of the longest palindrome
int longestPalSubstr(char *str)
{
    int maxLength = 1;  // The result (length of LPS)
 
    int start = 0;
    int len = strlen(str);
 
    int low, high;
 
    // One by one consider every character as center point of 
    // even and length palindromes
    for (int i = 1; i < len; ++i)
    {
        // Find the longest even length palindrome with center points
        // as i-1 and i.  
        low = i - 1;
        high = i;
        while (low >= 0 && high < len && str[low] == str[high])
        {
            if (high - low + 1 > maxLength)
            {
                start = low;
                maxLength = high - low + 1;
            }
            --low;
            ++high;
        }
 
        // Find the longest odd length palindrome with center 
        // point as i
        low = i - 1;
        high = i + 1;
        while (low >= 0 && high < len && str[low] == str[high])
        {
            if (high - low + 1 > maxLength)
            {
                start = low;
                maxLength = high - low + 1;
            }
            --low;
            ++high;
        }
    }
 
    printf("Longest palindrome substring is: ");
    printSubStr(str, start, start + maxLength - 1);
 
    return maxLength;
}
http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html

方法三:时间O(N) 空间O(N)

// Transform S into T.
// For example, S = "abba", T = "^#a#b#b#a#$".
// ^ and $ signs are sentinels appended to each end to avoid bounds checking
string preProcess(string s) {
  int n = s.length();
  if (n == 0) return "^$";
  string ret = "^";
  for (int i = 0; i < n; i++)
    ret += "#" + s.substr(i, 1);
 
  ret += "#$";
  return ret;
}
 
string longestPalindrome(string s) {
  string T = preProcess(s);
  int n = T.length();
  int *P = new int[n];
  int C = 0, R = 0;
  for (int i = 1; i < n-1; i++) {
    int i_mirror = 2*C-i; // equals to i' = C - (i-C)
    
    P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0;
    
    // Attempt to expand palindrome centered at i
    while (T[i + 1 + P[i]] == T[i - 1 - P[i]])
      P[i]++;
 
    // If palindrome centered at i expand past R,
    // adjust center based on expanded palindrome.
    if (i + P[i] > R) {
      C = i;
      R = i + P[i];
    }
  }
 
  // Find the maximum element in P.
  int maxLen = 0;
  int centerIndex = 0;
  for (int i = 1; i < n-1; i++) {
    if (P[i] > maxLen) {
      maxLen = P[i];
      centerIndex = i;
    }
  }
  delete[] P;
  
  return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
}
http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

相关文章推荐

C++/C求最长回文子串

  • 2010年07月31日 15:13
  • 1KB
  • 下载

hiho 1 最长回文子串

最长回文子串问题描述:一个字符串中连续的一段就是这个字符串的子串,而回文串指的是12421这种从前往后读和从后往前读一模一样的字符串,所以最长回文子串的意思就是这个字符串中最长的身为回文串的子串啦~ ...

最长回文子串c语言

  • 2013年06月26日 22:05
  • 2KB
  • 下载

最长回文子串

  • 2015年03月30日 21:01
  • 571B
  • 下载

求最长回文子串0(n)manacher算法

这个算法已经学了好几天,刚学的shihou
  • KJBU2
  • KJBU2
  • 2014年07月22日 17:05
  • 428

URAL 1297 Palindrome 后缀数组 或 Manacher 求最长回文子串

题目大意: 就是给出一个长度不超过1000个只包含大小写英文字母的字符串,输出其最长回文子串 大致思路: 首先很容易想到用将该字符串本身反转之后与自己连接起来, 中间用一个未出现的字符隔开, 求出...

hihoCoder #1032 : 最长回文子串

时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述    小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学...
  • Z_huing
  • Z_huing
  • 2016年10月22日 12:37
  • 347

字符串中查找最长回文子串完整代码

参考原文:http://blog.csdn.net/yzl_rex/article/details/7908259 #include #include #include int Min(int...

题目1528:最长回文子串

/* Mancher主算法。 学习地址:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 功能:求出以i为中心的回文半径p[i]; 参...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:最长回文子串
举报原因:
原因补充:

(最多只允许输入30个字)