-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossCountry.java
42 lines (40 loc) · 1.13 KB
/
CrossCountry.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
/** #dynamic-programming #lcs */
import java.util.Scanner;
class CrossCountry {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = Integer.parseInt(sc.nextLine());
for (int t = 0; t < testcase; t++) {
String girlLine = sc.nextLine();
String[] girlArr = girlLine.split(" ");
int result = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.equals("0")) {
System.out.println(result);
break;
} else {
String[] boyArr = line.split(" ");
result = Math.max(result, lcs(girlArr, boyArr));
}
}
}
}
public static int lcs(String[] arrA, String[] arrB) {
int n = arrA.length;
int m = arrB.length;
int[][] L = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || j == 0) {
L[i][j] = 0;
} else if (arrA[i - 1].equals(arrB[j - 1])) {
L[i][j] = 1 + L[i - 1][j - 1];
} else {
L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
}
}
}
return L[n - 1][m - 1];
}
}