-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDistinctSubsequences.java
70 lines (62 loc) · 2.53 KB
/
DistinctSubsequences.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class Main {
private BigInteger slowRecursive(String line, String subs) {
BigInteger[][] memo = new BigInteger[line.length()+1][subs.length()+1];
return slowRecursive(line, subs, memo);
}
private BigInteger slowRecursive(String line, String subs, BigInteger[][] memo) {
BigInteger count = BigInteger.ZERO;
if (memo[line.length()][subs.length()] != null) {
return memo[line.length()][subs.length()];
}
for (int i = line.length() - 1; i >= 0; --i) {
if (line.charAt(i) == subs.charAt(subs.length() - 1)) {
if (subs.length() == 1) {
count = count.add(BigInteger.ONE);
} else {
count = count.add(slowRecursive(line.substring(0, i),
subs.substring(0, subs.length() - 1), memo));
}
}
}
memo[line.length()][subs.length()] = count;
return count;
}
private BigInteger fastTabular(String line, String subs) {
BigInteger[][] memo = new BigInteger[line.length() + 1][subs.length() + 1];
for (int i = 0; i <= subs.length(); ++i) {
memo[0][i] = BigInteger.ZERO;
}
for (int i = 0; i <= line.length(); ++i) {
memo[i][0] = BigInteger.ZERO;
}
for (int i = 1; i <= subs.length(); ++i) {
for (int j = 1; j <= line.length(); ++j) {
if (line.charAt(j - 1) == subs.charAt(i - 1)) {
if (i == 1) {
memo[j][i] = memo[j - 1][i].add(BigInteger.ONE);
} else {
memo[j][i] = memo[j - 1][i].add(memo[j - 1][i - 1]);
}
} else {
memo[j][i] = memo[j - 1][i];
}
}
}
return memo[line.length()][subs.length()];
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < n; ++i) {
String line = reader.readLine().trim();
String subs = reader.readLine().trim();
Main solver = new Main();
//System.out.println(solver.slowRecursive(line, subs));
System.out.println(solver.fastTabular(line, subs));
}
}
}