Distinct Subsequences

来源:互联网 发布:淘宝客服是干什么的 编辑:程序博客网 时间:2024/06/16 17:15

http://fisherlei.blogspot.tw/2012/12/leetcode-distinct-subsequences_19.html


class Solution {public:    int numDistinct(string S, string T) {        if(S.empty())          return 0;        if(T.empty())          return 1;        vector<vector<int> > dp(S.length() + 1, vector<int>(T.length() + 1));        dp[0][0] = 1;        for(int i = 1; i <= S.length(); i++)          dp[i][0] = 1;        for(int i = 1; i <= T.length(); i++)          dp[0][i] = 0;        for(int i = 1; i <= S.length(); i++) {            for(int j = 1; j <= T.length(); j++) {                if(S[i - 1] != T[j - 1]) {                    dp[i][j] = dp[i - 1][j];                } else {                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];                }            }        }        return dp[S.length()][T.length()];    }};


0 0
原创粉丝点击