uva 10069 Distinct Subsequences (dp + 大數)
uva 10069 Distinct Subsequences
題目大意:給出兩個字符串A和B,找出A中所有與B相同的子字符串。
解題思路:if(A[j?1]==B[i?1]){
dp[i][j]=dp[i][j]+dp[i?1][j?1];
}
import java.math.BigInteger;
import java.util.Scanner;
/**
* Created by spzn on 15-3-30.
*/
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
BigInteger dp[][] = new BigInteger[105][10005];
int T, n, m;
String a, b;
T = cin.nextInt();
while (T-- > 0) {
a = cin.next();
b = cin.next();
n = a.length();
m = b.length();
for (int i = 0; i <= n; i++) {
dp[0][i] = BigInteger.ONE;
}
for (int i = 1; i <= m; i++) {
dp[i][0] = BigInteger.ZERO;
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i][j - 1];
if (a.charAt(j - 1) == b.charAt(i - 1)) {
dp[i][j] = dp[i][j].add(dp[i - 1][j - 1]);
}
}
}
System.out.println(dp[m][n]);
}
}
}