# 最长回文子串

// A dynamic programming solution for longest palindr.
// 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);
}

// 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

// 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

• 本文已收录于以下专栏：

## 动态规划算法求最长回文子串

• shineboyxxb
• 2016年07月31日 16:38
• 2731

## 【最长回文子串】Manache算法，O(N)时间复杂度

• zhong123123123
• 2016年05月06日 11:15
• 934

## C语言最长回文子串

• geshengtong
• 2017年10月28日 11:10
• 241

## hihoCoder#1032_最长回文子串

• sinat_30071459
• 2016年04月02日 19:37
• 1585

## hihocoder 1032 最长回文子串 (Manacher算法 详解+模板)

hihocoder 1032 最长回文子串 (Manacher算法 详解+模板)
• Tc_To_Top
• 2016年03月08日 21:11
• 657

## 最长回文子串（动态规划和递归）

• weijinqian0
• 2016年04月04日 10:24
• 2859

## 最长公共子串、最长公共子序列、最长回文子串、最长回文子序列、回文子串个数

1、最长公共子串 首先看最长公共子串的解答（暴力算法、动态规划、） 从优化到再优化，最长公共子串 2、最长公共子序列（LCS）  解析：动态规划解最长公共子序列问题 3、 leetcode ...
• m0_37693059
• 2017年08月04日 16:39
• 167

## 51nod oj 1088 1089 最长回文子串 【Manacher算法】

• leibniz_zhang
• 2016年08月10日 00:07
• 424

## hihocoder 1032 最长回文子串（Manachar算法）

#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述    小Hi和小Ho是一对好朋友，出生在信息化社会的他们对编程产生了莫大的兴...
• disparity_CJK
• 2016年09月10日 10:56
• 713

## 九度题目1528：最长回文子串

• u013517797
• 2014年04月27日 15:54
• 942

举报原因： 您举报文章：最长回文子串 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)