考虑两个字符串,我们用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

  1. import java.io.*;
  2. import java.math.*;
  3. import java.util.*;
  4. public class Main{
  5. public static void main(String []args){
  6. Scanner cin=new Scanner(System.in);
  7. int t=cin.nextInt();
  8. while(t--!=0){
  9. char a[]=cin.next().toCharArray();
  10. char b[]=cin.next().toCharArray();
  11. // System.out.println("2333 ");
  12. BigInteger [][] dp=new BigInteger[110][10100];
  13. for(int i=0;i<dp.length;i++){
  14. for(int j=0;j<dp[i].length;j++)
  15. dp[i][j]=BigInteger.ZERO;
  16. }
  17. // System.out.println("2333 ");
  18. for(int j=0;j<a.length;j++){
  19. if(j>0)
  20. dp[0][j]=dp[0][j-1];
  21. if(b[0]==a[j])
  22. dp[0][j]=dp[0][j].add(BigInteger.ONE);
  23. }
  24. // System.out.println("2333 ");
  25. for(int i=1;i<b.length;i++){
  26. for(int j=i;j<a.length;j++){
  27. dp[i][j]=dp[i][j-1];
  28. if(b[i]==a[j])
  29. dp[i][j]=dp[i][j].add(dp[i-1][j-1]);
  30. }
  31. }
  32. System.out.println(dp[b.length-1][a.length-1]);
  33. }
  34. }
  35. }

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. day02_12/12/2016_bean的实例化之普通工厂方式

  2. 如何下载JDK和JRE历史版本

    首先进入网址http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后页面滑到最下面,选择[Java Archive]后 ...

  3. ibatis知识点

    1:ibatis是apache的一个开源的项目,是一个O/R mapping解决方案,优点,小巧,灵活.2:搭建环境:导入ibatis相关jar包,jdbc驱动包等3:配置文件: jdbc连接的属性文 ...

  4. java设计模式03装饰者者模式

    动态地给一个对象添加一些额外的职责.就增加功能来说, Decorator模式相比生成子类更为灵活.该模式以对客 户端透明的方式扩展对象的功能. (1)在不影响其他对象的情况下,以动态.透明的方式给单个 ...

  5. HTTP常见状态码(404、400、500)等错误

    一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 详细分解: 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 ...

  6. SQL几种常用的函数

    函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...

  7. 9 Java 堆排序

    堆是具有以下性质的完全二叉树,每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆:或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆.如下图: 同时,我们对堆中的结点按层进行编号,将这种逻 ...

  8. netperf使用指南

    1. 介绍: Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf ...

  9. div+css 组织结构

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>家谱 ...

  10. 《啊哈算法》中P81解救小哈

    题目描述 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两 ...