zoukankan      html  css  js  c++  java
  • 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]);
    		  }
    	  }
      }


  • 相关阅读:
    MSSQL '20210806'转换成'2021-08-06'
    cxgrid 列内容居中显示
    CXGRID 导出EXCEL
    study PostgreSQL【3-get数据库中all表以及表的字段信息】
    study PostgreSQL【2-FireDAC连接PostgreSQL】
    高格-销售发票勾稽销售出货的赠品处理【14】
    study PostgreSQL【1-PostgreSQL对象】
    高格-负库存导致系统异常的处理【13】
    study Rust-9【组织管理】
    基础资料属性不符合目标组织要求:物料.允许库存,物料.来料检验
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6946353.html
Copyright © 2011-2022 走看看