-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS2.java
More file actions
38 lines (33 loc) · 1.09 KB
/
LCS2.java
File metadata and controls
38 lines (33 loc) · 1.09 KB
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
package nowcoder;
import java.util.Scanner;
/**
* Created by jacob on 2019-07-24.
*/
public class LCS2 {
public static void main(String[] args) {
LCS2 main = new LCS2();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String[] strings = scanner.nextLine().split(" ");
System.out.println(main.longestCommonSequence(strings[0], strings[1]));
}
}
private int longestCommonSequence(String a, String b) {
if (a.length() == 0 || b.length() == 0) {
return 0;
}
int[][] dp = new int[a.length() + 1][b.length() + 1];
char[] aChars = a.toCharArray();
char[] bChars = b.toCharArray();
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
if (aChars[i] == bChars[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]);
}
}
}
return dp[a.length()][b.length()];
}
}