UVA 10069 Distinct Subsequences(DP)

考虑两个字符串,我们用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]);
		  }
	  }
  }
时间: 2024-08-03 14:27:22

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

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

uva 10069 Distinct Subsequences 【dp+大数】

题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s ,求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的前 j 个字母中的出现次数: dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( x[ i ] == s [ j ] ) dp[i][j]=add(dp[i-1][j-1],dp[i][j]); 注意点:1:出现次数非常大,要用大数加法 2::注意初始化 AC代码:

UVa 10069 Distinct Subsequences(大数 DP)

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

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

UVA 10069 ---Distinct Subsequences +DP+大数

可以定义dp[i][j]表示第一个串的前i个字符中含有第二个串的前j个字符的总情况数: 则:如dp[i][j]=dp[i-1][j],如果str1[i]==str2[j]则dp[i][j]+=dp[i-1][j-1]; 初始时讲所有的dp[i][0]赋值为1,其他为0. 然后这个题目需要用到大数,可以用C++重载运算符,或者是java的大数类: 我用的是java,第一次用java的大数,感觉还不错 :) 代码如下: import java.util.*; import java.math.*;

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

UVA 580 - Critical Mass(DP)

题目链接:580 - Critical Mass 题意:一个栈,里面可以放L和U,有三个连续的U就是不安全的,问共有几种不安全的情况 思路:dp,dp[i][j][k],表示放到第i个,最后两个状态为j,k表示有没有出现不安全.然后去记忆化搜索一下就可以了 然后还有一种做法是,先考虑安全的情况,在用总情况(1<<n 种)减去安全的情况,安全的情况的递推方式很容易dp[i]表示放第i个, dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]. 不过这题都没给数据范围

UVA 11578 - Situp Benches(dp)

题目链接:11578 - Situp Benches 题意:健♂身♂房有两个仰卧起坐坐垫,每次调整角度要花费10元/10度,每次使用要花费15,现在给定n个人的时间顺序,和所希望的角度,求最少花费 思路:dp,dp[i][j][k]表示第i个人,一个角度为j,另一个为k的最小花费,一个人用和两个人用的情况分开讨论,然后记录dp状态转移路径.这个输出路径让这题变得麻烦了不少.不过机智的我还是把它搞♂出♂来♂了. 代码: #include <stdio.h> #include <string

uva10069 - Distinct Subsequences(大数+DP)

题目:uva10069 - Distinct Subsequences(大数+DP) 题目大意:给出字符串A , B.问能够在A中找到多少子串B,可以不连续. 解题思路:dp[i][j] 代表从i位开始的B串在从j位开始的A串中能够找到多少种. B[i] == A[j] dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]: B[i] != A[j] dp[i][j] = dp[i][j - 1]:边界要处理一下.就是B中只有最后的那个字符来和A匹配的情况要处理一