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匹配的情况要处理一下。

10^100。要用大数。

代码:

#include <cstdio>
#include <cstring>
#include <string>

const int N = 10005;
const int M = 105;
const int base = 100000;

char s1[N], s2[M];

int max (const int a, const int b) { return a > b ? a: b; }

struct bign {

	int len, s[30];
	bign () { memset (s, 0, sizeof (s));}
	bign (int num) { *this = num;}
	bign (const bign& b) { *this = b;}
	bign operator = (int num);
	bign operator + (const bign& b);
	bign operator + (const int b);
	bign operator += (const bign& b);
	void DelZore ();
} dp[M][N];

void bign::DelZore () {

	while (len >= 0 && s[len - 1] == 0) {
		len--;
	}
	if (len == 0)
		len = 1;
}

bign bign::operator = (int num) {

	if (num == 0) {

		len = 1;
		s[0] = 0;
	} else {

		len = 0;
		while (num > 0) {

			s[len++] = num % base;
			num = num / base;
		}
	}
	return * this;
}

bign bign::operator + (const bign& b) {

	bign c;
	c.len = 0;
	for (int i = 0, g = 0; g || i < max(len, b.len); i++) {

		int x = g;
		if (i < len) x += s[i];
		if (i < b.len) x += b.s[i];
		c.s[c.len++] = x % base;
		g = x / base;
	}
	return c;
}

bign bign::operator + (const int b) {

	bign b1;
	b1 = b;
	return *this + b1;
}

bign bign::operator += (const bign& b) {

	*this = *this + b;
	return *this;
}

int main () {

	int t, l1, l2;
	scanf ("%d%*c", &t);
	while (t--) {

		gets(s1);
		gets(s2);
		l1 = strlen (s1);
		l2 = strlen (s2);

		if (l2 == 0) {

			printf ("0\n");
			continue;
		}

		for (int i = 0; i < l2; i++) {
			dp[i][l1] = 0;
		}

		for (int i = l1 - 1; i >= 0; i--) {
			if (s1[i] == s2[l2 - 1])
				dp[l2 - 1][i] = dp[l2 - 1][i + 1] + 1;
			else
				dp[l2 - 1][i] = dp[l2 - 1][i + 1];
		}

		for (int i = l2 - 2; i >= 0; i--)
			for (int j = l1 - 1; j >= 0; j--) {

				dp[i][j] = dp[i][j + 1];
				if (s2[i] == s1[j])
					dp[i][j] += dp[i + 1][j + 1];
			}

		dp[0][0].DelZore();
		printf ("%d", dp[0][0].s[dp[0][0].len - 1]);
		for (int i = dp[0][0].len - 2; i >= 0; i--)
			printf ("%05d", dp[0][0].s[i]);
		printf ("\n");
	}
	return 0;
}
时间: 2025-01-18 13:53:21

uva10069 - Distinct Subsequences(大数+DP)的相关文章

[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

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代码:

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

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

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 [leetcode] DP,空间复杂度O(T.size)

求S中和T相同的子串的个数. 递推公式: dp(i,j):S[0...i-1]和T[0...j-1]中不同子串的个数. dp(0,j) = 0 dp(i,0) = 1 一般情况下 dp(i,j) = dp(i-1,j-1) + dp(i-1,j)    S[i-1] == T[j-1],保留S[i-1]以及丢弃S[i-1] = dp(i-1,j)                       S[i-1] != T[j-1],丢弃S[i-1],只比较S[0...j-2]和T[0...i-1] 因为

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]时

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

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