Distinct Subsequences - UVa 10069 dp+高精度
来源:互联网 发布:阿里云首页 编辑:程序博客网 时间:2024/06/07 01:21
Distinct Subsequences
Input: standard input
Output: standard output
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
2babgbag
bag
rabbbit
rabbit
Sample Output
53
题意:第二个字符串在第一个字符串中一共有多少种组合方式。
思路:dp[i][j]表示第一个字符串前i位,匹配第二个字符串的前j位一共有多少种匹配方式。if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j]+dp[i-1][j-1]; else dp[i][j]=dp[i-1][j];
然后此题答案比较大,要用到高精度……懒得打了,就用JAVA吧【其实你是不会打高精度→_→ 人艰不拆啊
AC代码如下:
import java.math.BigInteger;import java.util.Scanner;public class Main{ public static void main(String [] args) { Scanner scan=new Scanner(System.in);int t,i,j,k,len1,len2;String s1,s2; BigInteger dp[][]=new BigInteger[10010][110]; for(i=0;i<=10000;i++) for(j=0;j<=100;j++) dp[i][j]=BigInteger.ZERO; for(i=0;i<=10000;i++) dp[i][0]=BigInteger.ONE; t=scan.nextInt(); while(t>0) { t--; s1=scan.next(); s2=scan.next(); len1=s1.length(); len2=s2.length(); for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { if(s1.charAt(i-1)==s2.charAt(j-1)) dp[i][j]=dp[i-1][j].add(dp[i-1][j-1]); else dp[i][j]=dp[i-1][j]; } System.out.println(dp[len1][len2]); } }}
- dp+高精度 uva-10069-Distinct Subsequences
- UVA 10069 Distinct Subsequences(dp + 高精度)
- Distinct Subsequences - UVa 10069 dp+高精度
- UVA 10069 Distinct Subsequences(dp+高精度加法)
- Uva-10069 Distinct Subsequences DP
- UVA 10069 Distinct Subsequences(DP)
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
- uva 10069 - Distinct Subsequences(大数相加+DP)
- uva 10069 Distinct Subsequences(DP + 大数相加)
- uva 10069 Distinct Subsequences(dp,大数加法)
- UVa 10069 Distinct Subsequences(大数+DP)
- UVa 10069 Distinct Subsequences(大数 DP)
- uva 10069 Distinct Subsequences 【dp+大数】
- UVA 10069 ---Distinct Subsequences +DP+大数
- uva 10069 Distinct Subsequences (dp + 大数)
- Distinct Subsequences+uva+经典dp
- uva 10069 Distinct Subsequences
- UVA 10069 Distinct Subsequences
- 用于读取xml,解析然后按照规则来输出
- 在Mint 16中当按下光驱的弹出按钮后桌面上的光驱图标没有自动消失的问题解决
- Lambda函数
- Cocos2d-x CCEditBox & CCTextFieldTTF
- 如何把图片放入到页面的合适位置
- Distinct Subsequences - UVa 10069 dp+高精度
- hibernate批量处理数据
- MR素性检测算法
- html语法
- OCP 1Z0 053 230
- 使用android ProgressBar和Toast生成一个界面
- Linux I2C驱动分析(三)----i2c_dev驱动和应用层分析
- HTTP协议简介和多线程断点续传(转)
- (总结)CentOS Linux搭建SVN Server配置详解