-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathCode04_LongestPalindromicSubsequence.java
105 lines (97 loc) · 2.48 KB
/
Code04_LongestPalindromicSubsequence.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package class067;
// 最长回文子序列
// 给你一个字符串 s ,找出其中最长的回文子序列,并返回该序列的长度
// 测试链接 : https://leetcode.cn/problems/longest-palindromic-subsequence/
public class Code04_LongestPalindromicSubsequence {
// 最长回文子序列问题可以转化成最长公共子序列问题
// 不过这里讲述区间动态规划的思路
// 区间dp还会有单独的视频做详细讲述
public static int longestPalindromeSubseq1(String str) {
char[] s = str.toCharArray();
int n = s.length;
return f1(s, 0, n - 1);
}
// s[l...r]最长回文子序列长度
// l <= r
public static int f1(char[] s, int l, int r) {
if (l == r) {
return 1;
}
if (l + 1 == r) {
return s[l] == s[r] ? 2 : 1;
}
if (s[l] == s[r]) {
return 2 + f1(s, l + 1, r - 1);
} else {
return Math.max(f1(s, l + 1, r), f1(s, l, r - 1));
}
}
public static int longestPalindromeSubseq2(String str) {
char[] s = str.toCharArray();
int n = s.length;
int[][] dp = new int[n][n];
return f2(s, 0, n - 1, dp);
}
public static int f2(char[] s, int l, int r, int[][] dp) {
if (l == r) {
return 1;
}
if (l + 1 == r) {
return s[l] == s[r] ? 2 : 1;
}
if (dp[l][r] != 0) {
return dp[l][r];
}
int ans;
if (s[l] == s[r]) {
ans = 2 + f2(s, l + 1, r - 1, dp);
} else {
ans = Math.max(f2(s, l + 1, r, dp), f2(s, l, r - 1, dp));
}
dp[l][r] = ans;
return ans;
}
public static int longestPalindromeSubseq3(String str) {
char[] s = str.toCharArray();
int n = s.length;
int[][] dp = new int[n][n];
for (int l = n - 1; l >= 0; l--) {
dp[l][l] = 1;
if (l + 1 < n) {
dp[l][l + 1] = s[l] == s[l + 1] ? 2 : 1;
}
for (int r = l + 2; r < n; r++) {
if (s[l] == s[r]) {
dp[l][r] = 2 + dp[l + 1][r - 1];
} else {
dp[l][r] = Math.max(dp[l + 1][r], dp[l][r - 1]);
}
}
}
return dp[0][n - 1];
}
public static int longestPalindromeSubseq4(String str) {
char[] s = str.toCharArray();
int n = s.length;
int[] dp = new int[n];
for (int l = n - 1, leftDown = 0, backup; l >= 0; l--) {
// dp[l] : 想象中的dp[l][l]
dp[l] = 1;
if (l + 1 < n) {
leftDown = dp[l + 1];
// dp[l+1] : 想象中的dp[l][l+1]
dp[l + 1] = s[l] == s[l + 1] ? 2 : 1;
}
for (int r = l + 2; r < n; r++) {
backup = dp[r];
if (s[l] == s[r]) {
dp[r] = 2 + leftDown;
} else {
dp[r] = Math.max(dp[r], dp[r - 1]);
}
leftDown = backup;
}
}
return dp[n - 1];
}
}