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)的更多相关文章
- uva 10069 Distinct Subsequences 【dp+大数】
题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- UVa 10069 Distinct Subsequences(大数 DP)
题意 求母串中子串出现的次数(长度不超过1后面100个0 显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数 当a[i]==b[j]&am ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- 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 ...
- uva 116 Unidirectional TSP (DP)
uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...
- 13年山东省赛 Mountain Subsequences(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mountain Subsequences Time Limit: 1 Sec ...
- UVa 103 - Stacking Boxes(dp求解)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
随机推荐
- wxwidgets安装环境配置
一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...
- IMP-00058 ORA-12638:身份证明检索失败
需要将oracle的tns关掉 1.打开 oracle 的Net Manage 地址:开始 -> 程序 -> Oracle -> Configuration and Migratio ...
- c# regex Match Matches MatchCollection 用法
string text = "1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080"; Reg ...
- [转]linux tee 命令详解
转自: http://codingstandards.iteye.com/blog/833695 用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我 ...
- 《Java编程的逻辑》第四部分 文件
- JS——tab切换
排它思想: 1.先让所有的元素恢复默认值 2.再让选中的元素赋专有的值 3.干掉所有人,剩下我一个 <!DOCTYPE html> <html> <head lang=& ...
- 9、scala面向对象编程之继承
1. extends 2.override 和super 3.override field 4.isInstanceOf和asInstanceOf 5.getClass和classOf 6.使用模式 ...
- Logstash_Apache日志采集
[root@Cagios logstash-]# cat /usr/local/logstash-/logstash_agent.conf input { file { type => &quo ...
- windows下使用批处理设置环境变量
1. 设置临时环境变量 set BAT_HOME=c:\bat 此命令只对当前窗口有效,批处理或cmd窗口一关闭,变量就恢复原来的值了. 2. 设置永久环境变量 方法一 setx BAT_HOME C ...
- 关于JS中的方法是否加括号的问题
js中的方法什么时候加括号什么时候不加括号呢,我们有时候经常就搞不清楚,记住下面这几点就好理解了. 1.函数做参数时都不要加括号. function fun(a){ alert(a); } funct ...