动态规划之115 Distinct Subsequences

题目链接:https://leetcode-cn.com/problems/distinct-subsequences/description/

参考链接:https://www.cnblogs.com/springfor/p/3896152.html

     http://blog.csdn.net/abcbc/article/details/8978146

dp[i][j]:S使用前i个字符,T使用前面j个字符。dp[0][0]使用S前0个字符,使用T前0个字符。

当T为空的时候,空字符串是任何S串的字串。

当S为空的时候,任何字符都不是其的字串。

下图是S="rabbbit",T="rabbit"。

状态转移方程:

if (S.charAt(i - 1) != T.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j];
}
if (S.charAt(i - 1) == T.charAt(j - 1))
dp[i][j] = dp[i - 1][j]+dp[i - 1][j - 1];

如图所示:黄色部分是二者相等的,黑色部分是二者不想等的情况。

代码如下所示:

public int numDistinct(String S, String T) {
        int[][] dp = new int[S.length() + 1][T.length() + 1];
        dp[0][0] = 1;//initial

        for(int j = 1; j <= T.length(); j++)//S is empty
            dp[0][j] = 0;

        for (int i = 1; i <= S.length(); i++)//T is empty
            dp[i][0] = 1;

        for (int i = 1; i <= S.length(); i++) {
            for (int j = 1; j <= T.length(); j++) {
                if (S.charAt(i - 1) != T.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j];
                }
                if (S.charAt(i - 1) == T.charAt(j - 1))
                    dp[i][j] = dp[i - 1][j]+dp[i - 1][j - 1];
            }
        }

        return dp[S.length()][T.length()];
    }

原文地址:https://www.cnblogs.com/clarencezzh/p/10129444.html

时间: 2024-10-08 00:49:13

动态规划之115 Distinct Subsequences的相关文章

[动态规划] leetcode 115 Distinct Subsequences

problem:https://leetcode.com/problems/distinct-subsequences/ 字符匹配类型题目. class Solution { public: vector<vector<int>> dp; int numDistinct(string& s, string& t, int i, int j) { if(j == t.size()) return 1; if(i == s.size()) return 0; if(dp

115 Distinct Subsequences

115 Distinct Subsequences 这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数 class Solution: # @param {string} s # @param {string} t # @return {integer} def numDistinct(self, s, t): m, n = len(t), len(s) if m == 0 or n == 0: return 0 dp,cu

115. Distinct Subsequences(js)

115. Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the charac

[leedcode 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 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 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

115. Distinct Subsequences (String; DP)

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

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