Distinct Subsequences

来源:互联网 发布:爱淘宝红包入口 编辑:程序博客网 时间:2024/06/03 11:17

DP中的递推关系来自何方。。。。。。想不明白

//http://www.cnblogs.com/ganganloveu/p/3836519.html//http://www.cnblogs.com/kedebug/archive/2012/11/16/2772906.html//http://blog.csdn.net/fightforyourdream/article/details/17346385?reload#commentsclass Solution {public:    int numDistinct(string S, string T) {                int m = S.length();        int n = T.length();                vector<vector<int>>path(m+1,vector<int>(n+1,0));        for(int i = 0;i<m+1;i++)        {            //path[i][0]初始化为1的含义是:任何长度的S,如果转换为空串,那就只有删除全部字符这1种方式            path[i][0] = 1;        }        for(int i = 1;i<m+1;i++)        {            for(int j = 1;j < n+1;j++)            {                if(S[i-1] == T[j-1])                    path[i][j] = path[i-1][j-1]+path[i-1][j];                else                    path[i][j] = path[i-1][j];            }        }                return path[m][n];    }};


0 0
原创粉丝点击