|
|
|
@ -1,38 +1,75 @@
|
|
|
|
|
package class19;
|
|
|
|
|
|
|
|
|
|
// 测试链接:https://leetcode.com/problems/longest-palindromic-subsequence/
|
|
|
|
|
public class Code02_PalindromeSubsequence {
|
|
|
|
|
|
|
|
|
|
public static int lcse(char[] str1, char[] str2) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[][] dp = new int[str1.length][str2.length];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int longestPalindromeSubseq1(String s) {
|
|
|
|
|
if (s == null || s.length() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (s.length() == 1) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
char[] str = s.toCharArray();
|
|
|
|
|
char[] reverse = reverse(str);
|
|
|
|
|
return longestCommonSubsequence(str, reverse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static char[] reverse(char[] str) {
|
|
|
|
|
int N = str.length;
|
|
|
|
|
char[] reverse = new char[str.length];
|
|
|
|
|
for (int i = 0; i < str.length; i++) {
|
|
|
|
|
reverse[--N] = str[i];
|
|
|
|
|
}
|
|
|
|
|
return reverse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int longestCommonSubsequence(char[] str1, char[] str2) {
|
|
|
|
|
int N = str1.length;
|
|
|
|
|
int M = str2.length;
|
|
|
|
|
int[][] dp = new int[N][M];
|
|
|
|
|
dp[0][0] = str1[0] == str2[0] ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < str1.length; i++) {
|
|
|
|
|
dp[i][0] = Math.max(dp[i - 1][0], str1[i] == str2[0] ? 1 : 0);
|
|
|
|
|
for (int i = 1; i < N; i++) {
|
|
|
|
|
dp[i][0] = str1[i] == str2[0] ? 1 : dp[i - 1][0];
|
|
|
|
|
}
|
|
|
|
|
for (int j = 1; j < str2.length; j++) {
|
|
|
|
|
dp[0][j] = Math.max(dp[0][j - 1], str1[0] == str2[j] ? 1 : 0);
|
|
|
|
|
for (int j = 1; j < M; j++) {
|
|
|
|
|
dp[0][j] = str1[0] == str2[j] ? 1 : dp[0][j - 1];
|
|
|
|
|
}
|
|
|
|
|
for (int i = 1; i < str1.length; i++) {
|
|
|
|
|
for (int j = 1; j < str2.length; j++) {
|
|
|
|
|
for (int i = 1; i < N; i++) {
|
|
|
|
|
for (int j = 1; j < M; j++) {
|
|
|
|
|
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
|
|
|
if (str1[i] == str2[j]) {
|
|
|
|
|
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dp[str1.length - 1][str2.length - 1];
|
|
|
|
|
return dp[N - 1][M - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
|
public static int longestPalindromeSubseq2(String s) {
|
|
|
|
|
if (s == null || s.length() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (s.length() == 1) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
char[] str = s.toCharArray();
|
|
|
|
|
int N = str.length;
|
|
|
|
|
int[][] dp = new int[N][N];
|
|
|
|
|
dp[N - 1][N - 1] = 1;
|
|
|
|
|
for (int i = 0; i < N - 1; i++) {
|
|
|
|
|
dp[i][i] = 1;
|
|
|
|
|
dp[i][i + 1] = str[i] == str[i + 1] ? 2 : 1;
|
|
|
|
|
}
|
|
|
|
|
for (int i = N - 3; i >= 0; i--) {
|
|
|
|
|
for (int j = i + 2; j < N; j++) {
|
|
|
|
|
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
|
|
|
|
|
if (str[i] == str[j]) {
|
|
|
|
|
dp[i][j] = Math.max(dp[i][j], dp[i + 1][j - 1] + 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dp[0][N - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|