考虑两个字符串,我们用dp[i][j]表示字串第到i个和字符串到第j个的总数,由于字串必须连续

因此dp[i][j]能够有dp[i][j-1]和dp[i-1][j-1]递推而来,而不能由dp[i-1][j]递推而来。而后者的条件

是字串的第i个和字符串相等。

A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a sequence X =x1x2xm, another sequence Z = z1z2zk is
a subsequence of X if there exists a strictly increasing sequence <i1i2, …, ik> of indices of Xsuch that for all j =
1, 2, …, k, we have xij = zj. For example, Z = bcdb is a subsequence of X = abcbdab with corresponding index
sequence< 2, 3, 5, 7 >.

In this problem your job is to write a program that counts the number of occurrences of Z in X as a subsequence such that each has a distinct index sequence.

 

Input

The first line of the input contains an integer N indicating the number of test cases to follow.

The first line of each test case contains a string X, composed entirely of lowercase alphabetic characters and having length no greater than 10,000. The second line contains another string Z having length
no greater than 100 and also composed of only lowercase alphabetic characters. Be assured that neither Z nor any prefix or suffix of Z will have more than 10100 distinct occurrences in X as
a subsequence.

 

Output

For each test case in the input output the number of distinct occurrences of Z in X as a subsequence. Output for each input set must be on a separate line.

 

Sample Input

2

babgbag

bag

rabbbit

rabbit

 

Sample Output

5

3

import java.io.*;
import java.math.*;
import java.util.*;
public class Main{
public static void main(String []args){
Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
while(t--!=0){
char a[]=cin.next().toCharArray();
char b[]=cin.next().toCharArray();
// System.out.println("2333 ");
BigInteger [][] dp=new BigInteger[110][10100];
for(int i=0;i<dp.length;i++){
for(int j=0;j<dp[i].length;j++)
dp[i][j]=BigInteger.ZERO;
}
// System.out.println("2333 ");
for(int j=0;j<a.length;j++){
if(j>0)
dp[0][j]=dp[0][j-1];
if(b[0]==a[j])
dp[0][j]=dp[0][j].add(BigInteger.ONE);
}
// System.out.println("2333 ");
for(int i=1;i<b.length;i++){
for(int j=i;j<a.length;j++){
dp[i][j]=dp[i][j-1];
if(b[i]==a[j])
dp[i][j]=dp[i][j].add(dp[i-1][j-1]);
}
}
System.out.println(dp[b.length-1][a.length-1]);
}
}
}

UVA 10069 Distinct Subsequences(DP)的更多相关文章

  1. uva 10069 Distinct Subsequences 【dp+大数】

    题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...

  2. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  3. UVa 10069 Distinct Subsequences(大数 DP)

     题意 求母串中子串出现的次数(长度不超过1后面100个0  显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数   当a[i]==b[j]&am ...

  4. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

  5. 115. Distinct Subsequences (String; DP)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. uva 116 Unidirectional TSP (DP)

    uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...

  7. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  8. UVa 103 - Stacking Boxes(dp求解)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

随机推荐

  1. ACM_水题你要信了(修改版)

    水题你要信了 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发最近又认识了很多妹(han)子,可是妹(han)子一多不免有时会 ...

  2. 11 在C#中写文件

    在这个练习中,我们来学习如何把我们想要的东西写到文件中.我们在这个练习中还是使用File类中的方法来完成写文件的操作. 在这个练习中我们要用C#创建一个纯文本文件ex11.txt 放到c盘的Exerc ...

  3. 函数 out 传值 分割

    public void Jia(int a ,int b) { a = a + b; Console.WriteLine(a); } public void Jia1(int a,out int b) ...

  4. synchronized关键字详解(一)

    synchronized官方定义: 同步方法支持一种简单的策略防止线程干扰和内存一致性错误,如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都是通过同步方法完成的(这一个synchroniz ...

  5. 实现strcpy

    #include <stddef.h> char* strcpy(char* dest, const char* src) { if (dest == NULL || src == NUL ...

  6. Java—解压zip文件

    import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import ja ...

  7. Android 6.0一个完整的native service

     上一篇博客<Android 6.0 如何添加完整的系统服务(app-framework-kernel)>http://www.cnblogs.com/hackfun/p/7418902. ...

  8. 使用 Spring Social 连接社交网络

    Spring Social 框架是spring 提供社交平台的分享组件 https://www.ibm.com/developerworks/cn/java/j-lo-spring-social/

  9. [Windows Server 2008] 安装网站伪静态

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS伪静 ...

  10. json 新用

    如果使用struts2的action,可以省去属性赋值的工夫. 但是假如你没有使用struts2,而且使用的是ajax请求,通过json来传递参数.那我下面所说的对你可能是一个很好的解脱,从此告别re ...