当前位置:编程学习 > C/C++ >>

leetcode Distinct Subsequences DP

class Solution {
public:
    int numDistinct(string S, string T) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        vector< vector<int> > dp;
        vector<int> row(S.length()+1,0);
		vector<int> ones(S.length()+1,1);
        int i,j;
        
        if(S.length()<T.length())
            return 0;
        else{
			dp.push_back(ones);
            for(i=1;i<=T.length();i++)
                dp.push_back(row);			
            for(i=1;i<=T.length();i++){
                for(j=i;j<=S.length();j++)
                    if( T[i-1]==S[j-1] )
                        dp[i][j]=dp[i][j-1]+dp[i-1][j-1];
                    else
                        dp[i][j]=dp[i][j-1];
            }
        }
        return dp[T.length()][S.length()];
    }
};

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,