forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10069 Distinct Subsequences.java
52 lines (38 loc) · 1.56 KB
/
10069 Distinct Subsequences.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
import java.math.BigInteger;
import java.util.Scanner;
class Main
{
static final int MaxOverallSize = 10005, MaxSubstrSize = 105;
static BigInteger[][] numOccurances = new BigInteger[MaxOverallSize][MaxSubstrSize];
static String mainstr;
static String substr;
public static void main (String args[])
{
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int t = 1; t <= T; ++t) {
mainstr = scanner.next();
substr = scanner.next();
for (int i = 0; i < mainstr.length(); ++i)
for (int j = 0; j < substr.length(); ++j)
numOccurances[i][j] = null;
System.out.printf("%s\n", GetNumOccurances(0, 0).toString());
}
}
static BigInteger GetNumOccurances(int mainstrPos, int substrPos) {
if (substrPos == substr.length())
return BigInteger.ONE;
else if (mainstrPos == mainstr.length())
return BigInteger.ZERO;
if (numOccurances[mainstrPos][substrPos] == null)
{
BigInteger occurances = GetNumOccurances(mainstrPos + 1, substrPos);
if (mainstr.charAt(mainstrPos) == substr.charAt(substrPos))
{
occurances = occurances.add(GetNumOccurances(mainstrPos + 1, substrPos + 1));
}
numOccurances[mainstrPos][substrPos] = occurances;
}
return numOccurances[mainstrPos][substrPos];
}
}