uva 10069 Distinct Subsequences 【dp+大数】

题目:uva 10069 Distinct Subsequences

题意:给出一个子串 x 和母串 s ,求子串在母串中的不同序列的个数?

分析:定义dp【i】【j】:x 的前 i 个字母在 s 的前 j 个字母中的出现次数;

dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ;

if ( x[ i ] == s [ j ] )

dp[i][j]=add(dp[i-1][j-1],dp[i][j]);

注意点:1:出现次数非常大,要用大数加法

2::注意初始化

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 10050;
string dp[105][N];
string sum(string s1,string s2)
{
	if(s1.length()<s2.length())
	{
		string temp=s1;
		s1=s2;
		s2=temp;
	}
	int i,j;
	for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
	{
		s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));   //注意细节
		if(s1[i]-'0'>=10)
		{
			s1[i]=char((s1[i]-'0')%10+'0');
			if(i) s1[i-1]++;
			else s1='1'+s1;
		}
	}
	return s1;
}
int main()
{
    //freopen("Input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string s,x;
        cin>>s>>x;
        string ss="1",xx="2";
        ss+=s,xx+=x;
        for(int i=0;i<xx.size();i++)
            dp[i][0]="0";
        for(int i=0;i<ss.size();i++)
            dp[0][i]="1";
        for(int i=1;i<xx.size();i++)
        {
            for(int j=1;j<ss.size();j++)
            {
                dp[i][j]=dp[i][j-1];
                if(xx[i]==ss[j])
                    dp[i][j]=sum(dp[i-1][j-1],dp[i][j]);
                //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
            }

        }
        cout<<dp[xx.size()-1][ss.size()-1]<<endl;
    }
    return 0;
}
时间: 2024-08-24 03:09:16

uva 10069 Distinct Subsequences 【dp+大数】的相关文章

UVA 10069 ---Distinct Subsequences +DP+大数

可以定义dp[i][j]表示第一个串的前i个字符中含有第二个串的前j个字符的总情况数: 则:如dp[i][j]=dp[i-1][j],如果str1[i]==str2[j]则dp[i][j]+=dp[i-1][j-1]; 初始时讲所有的dp[i][0]赋值为1,其他为0. 然后这个题目需要用到大数,可以用C++重载运算符,或者是java的大数类: 我用的是java,第一次用java的大数,感觉还不错 :) 代码如下: import java.util.*; import java.math.*;

UVa 10069 Distinct Subsequences(大数 DP)

?? 题意 求母串中子串出现的次数(长度不超过1后面100个0  显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数   当a[i]==b[j]&&d[i-1][j-1]!=0时 d[i][j]=d[i-1][j-1]+d[i][j-1] (a[i]==b[j]时 子串前i个字母在母串前j个字母中出现的次数 等于 子串前i-1个字母在母串前j-1个字母中出现的次数 加上 子串前i个字母在母串前j-1个字母中出现的次数 a[i]!=b[j]时

uva 10069 Distinct Subsequences (dp + 大数)

uva 10069 Distinct Subsequences 题目大意:给出两个字符串A和B,找出A中所有与B相同的子字符串. 解题思路:if(A[j?1]==B[i?1]){ dp[i][j]=dp[i][j]+dp[i?1][j?1]; } import java.math.BigInteger; import java.util.Scanner; /** * Created by spzn on 15-3-30. */ public class Main { public static

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, give

Distinct Subsequences (dp)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

uva10069 - Distinct Subsequences(大数+DP)

题目:uva10069 - Distinct Subsequences(大数+DP) 题目大意:给出字符串A , B.问能够在A中找到多少子串B,可以不连续. 解题思路:dp[i][j] 代表从i位开始的B串在从j位开始的A串中能够找到多少种. B[i] == A[j] dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]: B[i] != A[j] dp[i][j] = dp[i][j - 1]:边界要处理一下.就是B中只有最后的那个字符来和A匹配的情况要处理一

Leetcode dp Distinct Subsequences

Distinct Subsequences Total Accepted: 15484 Total Submissions: 62324My Submissions Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original strin

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 of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative