-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1682.cpp
More file actions
25 lines (25 loc) · 993 Bytes
/
1682.cpp
File metadata and controls
25 lines (25 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
vector<vector<char>> edge(n, vector<char>(n));
for (int len = 2; len <= n; ++len) {
for (int left = 0, right = len - 1; right < n; ++left, ++right) {
if (s[left] == s[right] && (len == 2 || s[left] != edge[left + 1][right - 1])) {
dp[left][right] = dp[left + 1][right - 1] + 2;
edge[left][right] = s[left];
}
else if (dp[left + 1][right] > dp[left][right - 1]) {
dp[left][right] = dp[left + 1][right];
edge[left][right] = edge[left + 1][right];
}
else {
dp[left][right] = dp[left][right - 1];
edge[left][right] = edge[left][right - 1];
}
}
}
return dp[0][n - 1];
}
};