Distinct Subsequences 解题报告

题目:给两个字符串S和T,判断T在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.

-----------------------------------------------------

思路1:递归(TLE)

如果当前字符相同,结果加上S和T在该index之后的匹配方法数

如果当前字符不同,将S的指针向后移,递归计算

class Solution {
private:
    int cnt;
    int len_s;
    int len_t;
public:
    Solution():cnt(0){}
    void Count(string S,string T, int idx_ss, int idx_ts){
        if(idx_ts == len_t){
            cnt++;
            return;
        }
        int i,j,k;
        for (i=idx_ss; i<len_s; i++) {
            if (S[i] == T[idx_ts]) {
                Count(S, T, i + 1, idx_ts + 1);
            }
        }
    }

    int numDistinct(string S, string T) {
        len_s = S.length();
        len_t = T.length();
        Count(S, T, 0, 0);
        return cnt;
    }
};

-----------------------------------------------------

思路2:DP

如果当前字符相同,dp[i][j]结果等于用S[i](dp[i-1][j-1])和不用S[i](dp[i-1][j])方法数求和

如果当前字符不同,dp[i][j] = dp[i-1][j]

class Solution {
private:
    int len_s;
    int len_t;
public:
    int Count(string S,string T){
        int i,j;
        int dp[len_s][len_t];
        memset(dp, 0, sizeof(dp));

        if (S[0]==T[0]) {
            dp[0][0] = 1;
        }

        for(i=1;i<len_s;i++){
            dp[i][0] = dp[i-1][0];
            if (T[0]==S[i]) {
                dp[i][0]++;
            }
        }

        for (i=1; i<len_s; i++) {
            for (j=1; j<len_t && j<=i; j++) {
                if (S[i]!=T[j]) {
                    dp[i][j] = dp[i-1][j];
                    //cout<<dp[i-1][j]<<endl;
                }
                else{
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                    //dp[i-1][j-1]: use S[i], as S[i]==T[j]
                    //dp[i-1][j]  : don‘t use S[i]
                    //cout<<dp[i][j]<<endl;
                }
            }
        }
        return dp[len_s-1][len_t-1];
    }

    int numDistinct(string S, string T) {
        len_s = S.length();
        len_t = T.length();
        return Count(S, T);
    }
};
时间: 2024-10-28 19:14:34

Distinct Subsequences 解题报告的相关文章

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution 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

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 解题报告

[题目] 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 解题思路

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

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

[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

soj 1015 Jill&#39;s Tour Paths 解题报告

题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every year, Jill takes a bicycle tour between two villages. There are different routes she can take between these villages, but she does have an upper limit

leetCode解题报告5道题(十一)

题目一: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]

Distinct Subsequences——Leetcode

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