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/ ...
随机推荐
- BZOJ 2829 凸包
思路: 把信用卡周围去掉 只剩下中间的长方形 最后的答案加上一个圆 //By SiriusRen #include <bits/stdc++.h> using namespace std ...
- 阿里邮箱绑定Foxmail失败的解决办法
收件服务器地址: POP 服务器地址:pop3.mxhichina.com 端口110,SSL 加密端口995 或 IMAP 服务器地址:imap.mxhichina.com 端口143,SSL 加密 ...
- 修改docker-toolbox/boot2docker容器镜像
进入虚拟机 vi /var/lib/boot2docker/profile 编辑在EXTRA_ARGS,加入 --registry-mirror=https://pee6w651.mirror.ali ...
- Elasticsearch_Lucene基础
Lucene基本概念 文档(document):索引与搜索的主要载体,它包含一个或多个字段,存放将要写入索引的或将从索引搜索出来的数据. 字段(field):文档的一个片段,它包含字段的名称和字段的内 ...
- ElasticSearch-5.21安装
环境 操作系统:Centos 6.5 X64 IP地址:192.168.56.100 JDK 环境: # java -version java version "1.8.0_121" ...
- Sql Server 优化 SQL 查询:如何写出高性能SQL语句
1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来从一个 10万条记录的表中查1条 ...
- PHP 之websocket实现聊天室功能
一.功能界面 具体的详细代码:https://github.com/yangsphp/websocket-master/tree/master 二.具体代码实现 1.前端代码如下 <!DOCTY ...
- 零基础到精通Linux,从这篇文章开始
2018年想做Linux运维的人应该如何学习才能快速精通Linux? Linux入门这么简单,为什么很多人学不会? 想要成为一个合格的运维工程师,到底怎么才能从零开始精通Linux? 作为一个运维小白 ...
- 源码安装apache脚本
#!/bin/bash#By:zhaocheng#Date:2019-01-18 [ -d /media/cdrom ] || mkdir /media/cdrom[ -d /media/cdrom/ ...
- zTree 模糊搜索
/** * 搜索树,高亮显示并展示[模糊匹配搜索条件的节点s] * @param treeId * @param searchConditionId 搜索条件Id */ function search ...