Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
38 lines (33 loc) · 1.09 KB

File metadata and controls

38 lines (33 loc) · 1.09 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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()];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.