-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoveCalculator.java
More file actions
65 lines (63 loc) · 2 KB
/
LoveCalculator.java
File metadata and controls
65 lines (63 loc) · 2 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
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
/** #dynamic-programming #lcs #refactor #simple-way #optimized #todo */
import java.util.Scanner;
class LoveCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
for (int t = 1; t <= testcase; t++) {
String word1 = sc.next();
String word2 = sc.next();
int m = word1.length();
int n = word2.length();
int[][] L = new int[m + 1][n + 1];
long[][] numWays = new long[m + 1][n + 1];
lcs(word1, m, word2, n, L, numWays);
int len = m + n - L[m][n];
System.out.println("Case " + t + ": " + len + " " + numWays[m][n]);
}
}
public static void lcs(String word1, int m, String word2, int n, int[][] L, long[][] numWays) {
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
L[i][j] = 0;
numWays[i][j] = 1;
} else if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
L[i][j] = L[i - 1][j - 1] + 1;
numWays[i][j] = numWays[i - 1][j - 1];
} else {
L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
if (L[i][j] == L[i - 1][j]) {
L[i][j] = L[i - 1][j];
numWays[i][j] += numWays[i - 1][j];
}
if (L[i][j] == L[i][j - 1]) {
L[i][j] = L[i][j - 1];
numWays[i][j] += numWays[i][j - 1];
}
}
}
}
}
/*
// simple-way
findWays() {
for(int i=0; i <= m; i++) {
for(int j=0; j <=n; j++) {
if(i==0 || j==0) {
permutation[i][j] = 1;
}
else {
// permutation[i][j] = permutation[i-1][j] + permutation[i][j-1];
if( L[i][j] == L[i-1][j]) {
permutation[i][j] += permutation[i-1][j];
}
if (L[i][j] == L[i][j-1]) {
permutation[i][j] += permutation[i][j-1];
}
}
}
}
}
*/
}