-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLongest Common Subsequence.java
executable file
·213 lines (183 loc) · 6.99 KB
/
Longest Common Subsequence.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
M
1519197096
tags: DP, Double Sequence DP, Sequence DP
给两个string, A, B. 找这两个string里面的LCS: 最长公共字符长度 (不需要是continuous substring)
#### Double Sequence DP
- 设定dp长度为(n+1), 因为dp[i]要用来表示前i个(ith)时候的状态, 所以长度需要时i+1才可以在i位置, hold住i.
- 双序列: 两个sequence之间的关系, 都是从末尾字符看起, 分析2种情况:
- 1. A最后字符不在common sequence 或者 B最后字符不在common sequence.
- 2. A/B最后字符都在common sequence. 总体count + 1.
```
/*
Given two strings, find the longest comment subsequence (LCS).
Your code should return the length of LCS.
Example
For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1
For "ABCD" and "EACB", the LCS is "AC", return 2
Clarification
What's the definition of Longest Common Subsequence?
* The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science problem, the basis of file comparison programs such as diff, and has applications in bioinformatics.
* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Tags Expand
LintCode Copyright Longest Common Subsequence Dynamic Programming
*/
/*
Thoughts:
Sequence DP.
Common Subsequence: doesn't have to be conneccted subsequence.
Consider the last position of each string, there are 3 possible conditions:
1. A's last index is not part of common subsequence. Next step, consider: A[0 ~ n-1] and B
2. B's last index is not part of common subsequence Next step, consider: B[0 ~ n-1] and A
3. A's last index == B's last index, +1 on the result. Next step, consider: A[0 ~ n-1] and B[0 ~ n-1]
=> Each condition results in a sub problem.
dp[i][j]: longest common subsequence length for items: A[0 ~ i - 1] and B[0 ~ j - 1]
dp[i][j] = Max{dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1| A[i - 1]==B[i - 1] }
Space: O(MN)
Time: O(MN)
*/
/*
Thoughts:
dp[i][j] represent max LCS length for A[0, i - 1], B[0, j - 1]
Conditions:
- A[i-1] != B[j - 1]: no action
- A[i-1] == B[j - 1]: dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
*/
public class Solution {
public int longestCommonSubsequence(String A, String B) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return 0;
}
int m = A.length();
int n = B.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
// Init
if (i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
// Base condition: equals to previous's best
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
if (A.charAt(i - 1) == B.charAt(j - 1)) {
// match, take previous' best + 1
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
}
return dp[m][n];
}
}
// Optimization: Rolling array
// Space: O(N), Time: O(MN)
public class Solution {
public int longestCommonSubsequence(String A, String B) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return 0;
}
int m = A.length();
int n = B.length();
int[][] dp = new int[2][n + 1];
int curr = 1;
int prev = 0;
for (int i = 1; i <= m; i++) {
prev = curr;
curr = 1 - prev;
for (int j = 1; j <= n; j++) {
if(i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
dp[curr][j] = Math.max(dp[prev][j], dp[curr][j - 1]);
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[curr][j] = Math.max(dp[curr][j], dp[prev][j - 1] + 1);
}
}
}
return dp[curr][n];
}
}
// Print the longest common subsequence
public class Solution {
public int longestCommonSubsequence(String A, String B) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return 0;
}
int m = A.length();
int n = B.length();
int[][] dp = new int[m + 1][n + 1];
int[][] pi = new int[m + 1][n + 1]; // stores case1, case2, or case3
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if(i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
pi[i][j] = dp[i - 1][j] > dp[i][j - 1] ? 1 : 2;
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
if (dp[i][j] == dp[i - 1][j - 1] + 1) {
pi[i][j] = 3;
}
}
}
}
//Prepare for printing
char[] res = new char[dp[m][n]];
int i = m;
int j = n;
int w = dp[m][n] - 1;
while (i > 0 && j > 0) {
if (pi[i][j] == 1) {
i--;
} else if (pi[i][j] == 2) {
j--;
} else {//3
res[w] = A.charAt(i - 1);
i--;
j--;
w--;
}
}
for (int k = 0; k < dp[m][n]; k++) {
System.out.print(res[k]);
}
System.out.println();
return dp[m][n];
}
}
/*
Preivous Note.
Thinking process:
Using DP.
check[i][j] means: the length of longest common subsequnce between A(0 ~ i) and B(0 ~ j).
Then there are two ways to reach check[i][j]:
1. A(i-1) == B(j - 1), then check[i][j] = check[i - 1][j - 1] + 1;
2. A(i-1) != B(j - 1), then pick the max between (i-1,j) , (i,j-1) and (i, j )
Note: check[][] is initialized with all 0's. Index (0,0) is used as starting 0.
*/
public class Solution {
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
public int longestCommonSubsequence(String A, String B) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return 0;
}
int[][] check = new int[A.length() + 1][B.length() + 1];
for (int i = 1; i <= A.length(); i++) {
for (int j = 1; j <= B.length(); j++) {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
check[i][j] = check[i - 1][j - 1] + 1;
} else {
check[i][j] = Math.max(check[i][j], check[i - 1][j]);
check[i][j] = Math.max(check[i][j], check[i][j - 1]);
}
}
}
return check[A.length()][B.length()];
}
}
```