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 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.

  该题解析参考自LeetCode题解

  设状态为dp[i][j],表示T[0, j]在S[0, i]里出现的次数。首先,无论S[i]和T[j]是否相等,若不使用S[i],则dp[i][j]=dp[i-1][j];若S[i]=T[j],则可以使用S[i],此时dp[i][j]=dp[i-1][j]+dp[i-1][j-1]。

  代码如下:

 1 class Solution {
 2 public:
 3     int numDistinct(string s, string t) {
 4         int szS = s.size();
 5         int szT = t.size();
 6         if(szS < szT)
 7             return 0;
 8
 9         vector<vector<int> > dp(szS + 1, vector<int>(szT + 1, 0));
10         for(int i = 0; i < szS + 1; i++)
11             dp[i][0] = 1;
12
13         for(int i = 1; i < szS + 1; i++)
14             for(int j = 1; j < szT + 1; j++)
15             {
16                 if(s[i-1] != t[j-1])
17                     dp[i][j] = dp[i-1][j];
18                 else
19                     dp[i][j] = dp[i-1][j] + dp[i-1][j-1];
20             }
21
22         return dp[szS][szT];
23     }
24 };

   

时间: 2024-10-02 02:52:46

LeetCode之“动态规划”:Distinct Subsequences的相关文章

leetcode -day 15 Distinct Subsequences

1.  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 with

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

【Leetcode】115. Distinct Subsequences

Description: Given two string S and T, you need to count the number of T's subsequences appeared in S. The fucking problem description is so confusing. Input: String s and t output: The number Analysis: It's a dynamic processing problem. I drew the d

动态规划-Distinct Subsequences

2020-01-03 13:29:04 问题描述: 问题求解: 经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的. 有尝试过dfs来做,但是由于时间复杂度是指数级别的,所以会TLE. public int numDistinct(String s, String t) { int n1 = s.length(); int n2 = t.length(); int[][] dp = new int[n2 + 1][n1 + 1]; for (int i = 0;

[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

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 leetcode java

题目: 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 relat

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 之 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