LeetCode115 Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard)

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 positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

分析:

看题目感觉就跟LCS很像,考虑用双序列动态规划解决。

1. 状态:

dp[i][j]表示从第一个字符串前i个组成的子串转换为第二个字符串前j个组成的子串共有多少种方案。

2. 递推:

s[i - 1] == t[j - 1], 则dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];

s[i - 1] != t[j - 1],则dp[i][j] = dp[i - 1][j];

3. 初始化:

dp[i][0] = 1(删除到没有字符只有一种方案)

4. 返回值:

dp[sz1 - 1][sz2 - 1]

代码:

 1 class Solution {
 2 public:
 3     int numDistinct(string s, string t) {
 4         int sz1 = s.size(), sz2 = t.size();
 5         int dp[sz1 + 1][sz2 + 1];
 6         memset(dp, 0 , sizeof(dp));
 7         for (int i = 0; i < sz1; ++i) {
 8             dp[i][0] = 1;
 9         }
10         for (int i = 1; i <= sz1; ++i) {
11             for (int j = 1; j <= sz2; ++j) {
12                 if (s[i - 1] == t[j - 1]) {
13                     dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
14                 }
15                 else {
16                     dp[i][j] = dp[i - 1][j];
17                 }
18             }
19         }
20         return dp[sz1][sz2];
21     }
22 };
时间: 2024-12-29 23:53:55

LeetCode115 Distinct Subsequences的相关文章

Distinct Subsequences

https://leetcode.com/problems/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 non

[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 115. 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]Distinct Subsequences @ Python

原题地址:https://oj.leetcode.com/problems/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

leetcode 之 Distinct Subsequences

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 di

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

Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

Given a string S and a string T, count the number of distinct subsequences ofT inS. 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 po

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 笔记22 Distinct Subsequences 动态规划需要冷静

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 di