题目描述:

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

Example 1: Input:

1
2
"bbbab"

Output:

1
2
4

One possible longest palindromic subsequence is “bbbb”.

Example 2: Input:

1
2
"cbbd"

Output:

1
2
2

One possible longest palindromic subsequence is “bb”.

二维DP。dp[i][j]表示s[i]s[j](含两端)的字符串中最长的回文子串。状态转移方程如下:

  1. i==jdp[i][j]=1
  2. s[i]==s[j-1]dp[i][j]=2
  3. s[i]==s[j]dp[i][j]=dp[i+1][j-1]+2
  4. s[i]!=s[j]dp[i][j]=max(dp[i+1][j], dp[i][j-1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int longestPalindromeSubseq(string s) {
vector<vector<int>> dp(s.length(), vector<int>(s.length(), 0));
if (s.empty()) return 0;
int maxLen = 1;
for (int i = 0; i < s.length(); i++) {
dp[i][i] = 1;
}
for (int i = s.length() - 1; i >= 0; i--) {
for (int j = i + 1; j < s.length(); j++) {
if (s[i] == s[j]) {
if (i + 1 == j) dp[i][j] = 2;
else dp[i][j] = dp[i + 1][j - 1] + 2;
}
else {
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][s.length() - 1];
}
};