考虑两个字符串,我们用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 103 - Stacking Boxes(dp求解)

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

  2. Uva 1625 - Color Length(DP)

    题目链接 https://cn.vjudge.net/problem/UVA-1625 [题意] 输入两个长度分别为n和m的颜色序列(n,m<=5000),要求按一定规则合并成一个序列,规则是每 ...

  3. ☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)

    题干: You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music ...

  4. UVA - 12260 Free Goodies(dp比较难想到)

    题目链接:uva12260 题目大意:有n个糖果,每个糖果有p,j两个值,现在有两个人Petra和Jan,Prtra的取糖果方式是优先去p值大的j值小的:Jan取糖果的方式是尽量让自己开心值(取出糖果 ...

  5. UVA - 12563 劲歌金曲(DP 01背包)

    题目 KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了(每首歌时长不超过3min). 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , t实际不会超过(18 ...

  6. UVA 11578 - Situp Benches(dp)

    题目链接:11578 - Situp Benches 题意:健♂身♂房有两个仰卧起坐坐垫,每次调整角度要花费10元/10度,每次使用要花费15,如今给定n个人的时间顺序,和所希望的角度,求最少花费 思 ...

  7. 【UVa】Wavio Sequence(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. UVA 662 Fast Food(dp)

    题意: 一条直线马路上有n个餐馆,各个餐馆的坐标为di. 现在要在这n个餐馆中选择k个餐馆用来建造仓库. 没有仓库的餐馆,只能使用附近最近的一个仓库. 问总距离最少的建造方案,并输出. 思路: 先进行 ...

  9. P7888-「MCOI-06」Distinct Subsequences【dp】

    正题 题目大意 给出一个长度为nnn的字符串aaa,求它的所有子序列的本质不同子序列个数. 1≤n≤1061\leq n\leq 10^61≤n≤106 解题思路 考虑每个子序列产生的贡献,为了防止算 ...

最新文章

  1. IE兼容CSS3圆角border-radius,box-shadow,text-shadow的方法
  2. 同时tar解压多个文件
  3. 模拟上帝之手的对抗博弈——GAN背后的数学原理
  4. 【PHP】安装 ssh2 模块
  5. Modify批量处理优化
  6. python如何注释掉一段代码_爬取出来的网页代码都被注释掉怎么解决?
  7. opencv:边缘检测之Laplacian算子思想及实现
  8. 安装mysql 5.1 详细步骤
  9. 卡巴斯基2013简体中文正式版
  10. 电力拖动自动控制系统复习(一)
  11. Android源码下载(包括最新8.0版本)
  12. 2017大一计算机教程,2017年计算机等考一级章节考点:WPS2000新手入门教程
  13. python修改word文本框中的内容
  14. rstudio中读取数据_用RStudio导入数据
  15. Scratch快速入门(一)
  16. 好生猛,游戏里爆肝创纪录,太强了!
  17. linux服务器新装hba卡,EmulexHBA卡在Linux下的安装方法
  18. APP代码打包成apk文件
  19. arcgis 提取值到点 如果该点的栅格值缺失怎么办?怎么把最近点的值赋给它?
  20. 小程序空格解决文字分散对齐

热门文章

  1. mysql 索引都有哪些_MySQL 数据库的优化,你知道有哪些?
  2. java 桥梁模设计,Java设计模式学习篇(九)桥接设计模式
  3. python3字典平均值_python3基础之字典
  4. html5引入外联样式的优先级,CSS的4种引入方式及优先级
  5. 嵌入式linux开发考试题目,练一练!3道经典嵌入式Linux面试题,答案在文末。
  6. python某行某列读取数据_使用scrpython从某行的第一列提取数据
  7. 容器可以作为全局变量吗_四季青是风水树吗?哪些可以作为风水树?
  8. vue 传递 对象 路由_vue 04 -vue路由对象($route)参数简介以及和router的区别
  9. 二进制编译安装mysql_MariaDB通用二进制编译安装详解
  10. numpy数组基础语法