forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS2.java
70 lines (60 loc) · 1.95 KB
/
LCS2.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
/// LCS问题
/// 动态规划
/// 时间复杂度: O(len(s1)*len(s2))
/// 空间复杂度: O(len(s1)*len(s2))
public class LCS2 {
public String lcs(String s1, String s2){
int m = s1.length();
int n = s2.length();
// 对memo的第0行和第0列进行初始化
int[][] memo = new int[m][n];
for(int j = 0 ; j < n ; j ++)
if(s1.charAt(0) == s2.charAt(j)){
for(int k = j ; k < n ; k ++)
memo[0][k] = 1;
break;
}
for(int i = 0 ; i < m ; i ++)
if(s1.charAt(i) == s2.charAt(0)) {
for(int k = i ; k < m ; k ++)
memo[k][0] = 1;
break;
}
// 动态规划的过程
for(int i = 1 ; i < m ; i ++)
for(int j = 1 ; j < n ; j ++)
if(s1.charAt(i) == s2.charAt(j))
memo[i][j] = 1 + memo[i-1][j-1];
else
memo[i][j] = Math.max(memo[i-1][j], memo[i][j-1]);
// 通过memo反向求解s1和s2的最长公共子序列
m = s1.length() - 1;
n = s2.length() - 1;
StringBuilder res = new StringBuilder("");
while(m >= 0 && n >= 0)
if(s1.charAt(m) == s2.charAt(n)){
res.insert(0, s1.charAt(m));
m --;
n --;
}
else if(m == 0)
n --;
else if(n == 0)
m --;
else{
if(memo[m-1][n] > memo[m][n-1])
m --;
else
n --;
}
return res.toString();
}
public static void main(String[] args) {
String s1 = "ABCDGH";
String s2 = "AEDFHR";
System.out.println((new LCS2()).lcs(s1, s2));
s1 = "AAACCGTGAGTTATTCGTTCTAGAA";
s2 = "CACCCCTAAGGTACCTTTGGTTC";
System.out.println((new LCS2()).lcs(s1, s2));
}
}