Distinct Subsequences+uva+经典dp
2014-09-30 12:30
423 查看
Distinct Subsequences
Description
Problem E
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 X such 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
解决方案:想了一天没思路,看了别人的博客才懂的。求有多少个不同位置的相同子串,可以转为求有多少种删掉字符变成目标子串的方法。
dp[i][j]代表把串i变成串j的方法数,若text1[i]==text2[j],字符i有两种情况,用则
dp[i][j]+=dp[i-1][j-1],若不用dp[i][j]+=dp[i-1][j],所以dp[i][j]=dp[i-1][j-1]+dp[i-1][j]。若text1[i]!=text2[j]则字符i必不用,dp[i][j]=dp[i-1][j];
边界条件为:dp[i][0]=1,即把字符变空串的方法只有一个。此题要用到大数,所以用java来写。
code:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Problem E
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 X such 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
解决方案:想了一天没思路,看了别人的博客才懂的。求有多少个不同位置的相同子串,可以转为求有多少种删掉字符变成目标子串的方法。
dp[i][j]代表把串i变成串j的方法数,若text1[i]==text2[j],字符i有两种情况,用则
dp[i][j]+=dp[i-1][j-1],若不用dp[i][j]+=dp[i-1][j],所以dp[i][j]=dp[i-1][j-1]+dp[i-1][j]。若text1[i]!=text2[j]则字符i必不用,dp[i][j]=dp[i-1][j];
边界条件为:dp[i][0]=1,即把字符变空串的方法只有一个。此题要用到大数,所以用java来写。
code:
import java.util.*; import java.math.*; public class Main { public static void main(String arg[]){ int t; int maxn=10004; Scanner cin=new Scanner(System.in); t=cin.nextInt(); for(int ii=1;ii<=t;ii++){ String text1; String text2; text1=cin.next(); text2=cin.next(); BigInteger dp[][]=new BigInteger[maxn][120]; BigInteger temp; int len1=text1.length(); int len2=text2.length(); if(len1<len2) { System.out.print("0"); System.out.println(); continue; } for(int i=0;i<=len1;i++) for(int j=0;j<=len2;j++) dp[i][j]=BigInteger.ZERO; for(int i=0;i<=len1;i++){ dp[i][0]=BigInteger.ONE; } for(int i=1;i<=len1;i++) for(int j=1;j<=len2;j++){ dp[i][j]=dp[i-1][j]; if(text1.charAt(i-1)==text2.charAt(j-1)){ dp[i][j]=dp[i][j].add(dp[i-1][j-1]); } } System.out.print(dp[len1][len2]); System.out.println(); } } }
相关文章推荐
- Uva-10069 Distinct Subsequences DP
- UVA 10069 ---Distinct Subsequences +DP+大数
- UVa 10617 Again Palindrome(经典回文串区间DP)
- UVA1626 DP经典
- leetcode Distinct Subsequences DP
- UVA 1347 Tour (经典DP~)
- UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)
- UVA 10934 Dropping water balloons(经典DP)
- UVA 10534 - Wavio Sequence(经典dp)
- uva 674 Coin Change 经典dp入门
- UVA 10891 Game of Sum(经典区间dp)
- uva 10069 - Distinct Subsequences
- UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)
- UVa 10739 String to Palindrome(经典回文串区间DP)
- UVA - 1427(经典滑动队列优化dp)
- UVA 674 Coin Change 换硬币 经典dp入门题
- UVALive 2038 - Strategic game (经典树形DP)
- UVA 348 经典dp
- uva 662 (经典DP邮局问题)
- UVA 11270 Tiling Dominoes(轮廓线DP经典)