【leetcode】Distinct Subsequences

问题:给定两个字符串S,T,对于S,可以删除其中的任意多个(包括0)字符,使其得到T。问有多少种删法可以得到T。

举例分析:

S:ababa

T: aba

dp[i][j] : 表示 S 从0 ~ i - 1,T从0~j - 1,所得到的方法数。i,j 表示长度。

初始条件:dp[i][0] = 1,T为空串,而空串总是任意串的字串。即,将S串的所有字符都删掉,就得到T。

状态转移方程:

dp[ i ] [ j ] = dp[ i - 1] [ j - 1] + dp[ i - 1] [ j ]  ; if  S[i - 1] == T[ j - 1]

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

第一个状态转移方程表示,当S[i - 1] == T[ j - 1]时,我可以将T[j - 1] 认为是从S[i - 1] 得到,也可以认为不是。这是两种情况,最后的结果应该是两种情况的和。

按着状态转移方程,我们通过填表,得到结果如上图所示,其中第一列黑色的 1 为初始状态。每一行相同的颜色是我们通过一次遍历得到的结果。最终的结果为 右下角的 4.

//code

int numDistinct( string S,  string T){

	int lenT = T.size();
	int lenS = S.size();
	if(lenS < lenT)  return 0;
	vector<vector<int> > dp(lenS + 1, vector<int>(lenT + 1, 0));
	for (int i = 0; i <= lenS; ++i)
	    dp[i][0] = 1;
	for (int i = 1; i <= lenS; ++i)
		for(int j = 1; j <= lenT; ++j)
		{
			if(T[j - 1] == S[i - 1])
				dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
			else
				dp[i][j] = dp[i - 1][j];
		}

	return dp[lenS][lenT];
}

通过状态转移方程,我们发现, 第 i 行只跟第 i - 1 行有关系,所以可以简化dp表,只用两行来进行迭代。但是这样每一行的第一个元素都很特别,都是 1 ,就使得我们在迭代的时候稍微麻烦些,我们换一下行列。当然我们也可以按列填表,但是我们总是习惯按行处理,至少笔者如此。

如此一来,所有的初始化就在同一行了,

int numDistinct(string S, string T) {

	int lenT = T.size();
	int lenS = S.size();
	if(lenS < lenT)  return 0;

	//dp[0][i] = 1.
	vector<int> dp_pre(lenS + 1, 1);

	for (int i = 1; i <= lenT; ++i)
	{
		vector<int> dp_cur(lenS + 1, 0);
		for (int j = 1; j <= lenS; ++j)
		{
			if(T[i - 1] == S[j - 1])
				dp_cur[j] = dp_pre[j - 1] + dp_cur[j - 1];
			else
				dp_cur[j] = dp_cur[j - 1];
		}
		dp_pre = dp_cur;
	}
	return dp_pre[lenS];

}

【leetcode】Distinct Subsequences,布布扣,bubuko.com

时间: 2024-08-07 12:32:00

【leetcode】Distinct Subsequences的相关文章

【LeetCode】Distinct Subsequences 解题报告

[题目] 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 rela

【leetcode】Distinct Subsequences(hard)

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】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【leetcode刷题笔记】Distinct Subsequences

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】N-queens

问题: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"