代码随想录算法训练营第五十五天| LeetCode392.判断子序列 115.不同的子序列

392.判断子序列

题目:https://leetcode.cn/problems/is-subsequence/description/

class Solution {
public:
    bool isSubsequence(string s, string t) {
        vector> dp(s.size()+1,vector(t.size()+1,0));
        for(int i = 1; i <= s.size(); ++i){
            for(int j = 1; j <= t.size(); ++j){
                if(s[i-1] == t[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else{
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
                
            }
        }
        return dp[s.size()][t.size()] == s.size();
    }
};

115.不同的子序列

题目:力扣

class Solution {
public:
    int numDistinct(string s, string t) {
        vector> dp(s.size()+1,vector(t.size()+1,0));
        for(int i = 0; i <= s.size(); ++i) dp[i][0] = 1;
        for(int i = 1; i <= s.size(); ++i){
            for(int j = 1; j <= t.size(); ++j){
                if(s[i-1] == t[j-1] ){
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[s.size()][t.size()];
    }
};

总结

题型:动态规划,子序列问题

你可能感兴趣的:(代码随想录算法训练营,leetcode,算法,动态规划,c++)