-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ_LongestCommonSubsequence.java
88 lines (85 loc) · 2.6 KB
/
Q_LongestCommonSubsequence.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
/**
* @author wmy
* @date 2021/6/13 21:45
*/
/*
描描述
给定两个字符串s1和s2,输出两个字符串的最长公共子序列长度
示例1
输入:
"1AB2345CD","12345EF"
返回值:
5
*/
public class Q_LongestCommonSubsequence {
/**
* longest common subsequence
* <p>
* 参考:https://blog.csdn.net/ggdhs/article/details/90713154
* 二维动态规划(分两步进行)
* <p>
* 第一步:计算转移矩阵
* dp[i][j]:s1[0:i]和s2[0:j]的最长公共子序列
* 状态转移方程:
* if(s1[i]==str[j]):
* dp[i][j] = dp[i-1][j-1]+1
* else:
* dp[i][j]=max(dp[i-1][j],dp[i,j-1])
* <p>
* 第二步:往前回溯寻找最长子序列
*
* @param s1 string字符串 the string
* @param s2 string字符串 the string
* @return string字符串
*/
public String LCS(String s1, String s2) {
// write code here
//step1
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
char c1 = s1.charAt(i - 1);
for (int j = 1; j <= n; j++) {
char c2 = s2.charAt(j - 1);
if (c1 == c2) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
//step2
return getLongestCommonSubsequence(dp, s1, s2, m, n);
}
public String getLongestCommonSubsequence(int[][] dp, String s1, String s2, int m, int n) {
int maxLen = dp[m][n];
if (maxLen == 0) {
return "-1";
}
StringBuilder stringBuilder = new StringBuilder();
for (int k = maxLen; k > 0; k--) {
while (dp[m][n] == k) {
if (s1.charAt(m - 1) == s2.charAt(n - 1)) {
stringBuilder.append(s1.charAt(m - 1));
m--;
n--;
} else {
if (dp[m - 1][n] == k) {
m--;
} else if (dp[m][n - 1] == k) {
n--;
}
}
}
}
return stringBuilder.reverse().toString();
}
public static void main(String[] args) {
Q_LongestCommonSubsequence app = new Q_LongestCommonSubsequence();
String s1 = "abc";
String s2 = "def";
String res = app.LCS(s2, s1);
System.out.println(res);
}
}