题目要求
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
解题思路
动态规划法
解题思路
在这道题中,采用自底向上的思想,记 S S S为输入字符串, D C [ i ] [ j ] , 0 ≤ i ≤ S . l e n g t h − 1 , i ≤ j ≤ S . l e n g t h − 1 DC[i][j], 0\le i\le S.length-1, i\le j\le S.length-1 DC[i][j],0≤i≤S.length−1,i≤j≤S.length−1表示 S [ i . . . j ] S[i...j] S[i...j]这个子串的最长回文子串的长度,我们需要求的就是 D C [ 0 ] [ S . l e n g t h − 1 ] DC[0][S.length - 1] DC[0][S.length−1]所对应的一个字符串,即最长回文子串。
为了最后输出最长回文子串,我们在动态规划的过程中需要记录最后要输出的字符串,因此我们采用矩阵 S P [ ] [ ] SP[][] SP[][]来表示:
- S P [ i ] [ j ] = = 3 SP[i][j] == 3 SP[i][j]==3对应将 i + + i++ i++, j − − j-- j−−
- S P [ i ] [ j ] = = 2 SP[i][j] == 2 SP[i][j]==2对应将 i + + i++ i++
- S P [ i ] [ j ] = = 1 SP[i][j] == 1 SP[i][j]==1对应将 j − − j-- j−−
- S P [ i ] [ j ] = = 0 SP[i][j] == 0 SP[i][j]==0则终止
在动态规划的过程中,我们首先将 D C DC DC里面的全部元素都初始化为 0 0 0,然后令 D C [ i ] [ i ] = 1 , 0 ≤ i ≤ S . l e n g t h − 1 DC[i][i] = 1, 0\le i\le S.length-1 DC[i][i]=1,0≤i≤S.length−1,即单个元素组成一个长度为1的回文子串。然后我们使用如下的递推式来求出 D C [ 0 ] [ S . l e n g t h − 1 ] DC[0][S.length - 1] DC[0][S.length−1]:
D C [ i ] [ j ] = { D C [ i + 1 ] [ j − 1 ] + 2 , i f S [ i ] = = S [ j ] max { D C [ i + 1 ] [ j ] , D C [ i ] [ j − 1 ] } , e l s e DC[i][j]=\left\{ \begin{aligned} &DC[i+1][j-1]+2 & , & if&S[i]==S[j] \\ &\max\{DC[i+1][j],DC[i][j-1]\} & ,& else \end{aligned} \right. DC[i][j]={DC[i+1][j−1]+2max{DC[i+1][j],DC[i][j−1]},,ifelseS[i]==S[j]
在这个过程中根据判断条件来更新 S P SP SP矩阵,最终输出结果,这部分更新的准则在下面的源代码中能够体现出来。
源代码
class Solution {
public:
string longestPalindrome(string s) {
// 如果是空字符串的话直接返回""
// 因为空字符串不存在回文子串
if (s == "") {
return "";
}
// Get the length of input string s
int s_length = s.length();
// matrix for DC
int **DC = new int *[s_length];
for (int i = 0; i < s_length; i++) {
DC[i] = new int[s_length];
}
// initialize the DC
for (int i = 0; i < s_length; i++) {
for (int j = 0; j < s_length; j++) {
if (i != j) {
DC[i][j] = 0;
} else {
DC[i][j] = 1;
}
}
}
// matrix for store the LP
int **SP = new int *[s_length];
for (int i = 0; i < s_length; i++) {
SP[i] = new int[s_length];
}
// initialize the LP
for (int i = 0; i < s_length; i++) {
for (int j = 0; j < s_length; j++) {
SP[i][j] = 0;
}
}
// 开始进行自底向上的动态规划
// 注意SP矩阵的更新规则
for (int i = 1; i < s_length; i++) {
for (int j = 0; j < s_length - i; j++) {
// left index: j
// right index: j + i
if (s.at(j) == s.at(j + i)
&& (SP[j + 1][j + i - 1] == 3
|| SP[j + 1][j + i - 1] == 0)) {
SP[j][j + i] = 3;
DC[j][j + i] = DC[j + 1][j + i - 1] + 2;
} else {
if (DC[j + 1][j + i] > DC[j][j + i - 1]) {
SP[j][j + i] = 2;
DC[j][j + i] = DC[j + 1][j + i];
} else {
SP[j][j + i] = 1;
DC[j][j + i] = DC[j][j + i - 1];
}
}
}
}
// c_out用来判断s中哪些字符需要输出
bool *c_out = new bool[s_length];
// intialize the c_out
for (int i = 0; i < s_length; i++) {
c_out[i] = false;
}
// 用来获取需要输出的字符
int i = 0;
int j = s_length - 1;
while (SP[i][j] != 0) {
if (SP[i][j] == 3
&& (SP[i + 1][j - 1] == 3
|| SP[i + 1][j - 1] == 0)) {
c_out[i++] = true;
c_out[j--] = true;
} else if (SP[i][j] == 2) {
i++;
} else {
j--;
}
}
c_out[i] = true;
// out中存放最后返回的最长回文子串
string out = "";
for (int i = 0; i < s_length; i++) {
if (c_out[i]) {
out += s[i];
}
}
return out;
}
};