日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA 10069 Distinct Subsequences(DP)

發布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?=x1x2…xm, another sequence?Z?=?z1z2…zk?is a subsequence of?X?if there exists a strictly increasing sequence?<i1,?i2, …,?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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。