LeetCode-097-字符串-子序列-子序列的数目

从上到下的动态规划,并且配合备忘录进行剪枝操作。

class Solution {
public:
    //dp[i][j]表示 t的前j个元素构成的字符串 在 s的前i个元素构成的字符串 中出现的个数
    vector<vector<int>> dp;

    int numDistinct(string s, string t) {
        //方法一:dfs超出时间限制
        // int n = s.length(), m = t.length();
        // if (m == 0)
        //     return 1; 

        // if (n < m)
        //     return 0;

        // vector<int> path;
        // vector<vector<int>> res;
        // dfs(s,t,0,path,res);

        // return res.size();

        //方法二:处理后的 dfs,仍然是超时的
        // int n = s.length(), m = t.length();
        // if (m == 0)
        //     return 1; 

        // if (n < m)
        //     return 0;

        // int count = 0;
        // vector<vector<int>> cnt(64);
        // for (int i=0;i<n;++i)
        //     cnt[s[i]-'A'].push_back(i);

        // vector<int> path;
        // vector<vector<int>> res;
        // dfs2(cnt,t,path,res);

        // return res.size();

        //方法三:从上到下(将大问题转化为小问题的递归求解)的动态规划
        int n = s.length(), m = t.length();
        if (n < m)
            return 0;
        else if (n == m)
            return s==t ? 1 : 0; 

        if (m == 0)
            return 1;  

        if (dp.empty())
            dp.assign(n+1, vector<int>(m+1, -1));

        if (-1 != dp[n][m])
            return dp[n][m];

        //从后向前在“源串”中查找第一个等于“模式串”最后一个字符的索引
        for (int j=n-1;j>=0;--j)
        {
            if (s[j] != t[m-1])
                continue;
            // dp[i][j]表示 t的前j个元素构成的字符串 在 s的前i个元素构成的字符串 中出现的个数
            //状态转移方程:当 s[n-1] == t[m-1] 时,dp[n][m] = dp[n-1][m-1] + dp[n-1][m]
            dp[n][m] = numDistinct(s.substr(0,j), t.substr(0,m-1)) + numDistinct(s.substr(0,j), t);
            break;
        }
        
        // dp[n][m] = -1,说明上面的for循环没有执行到 dp[n][m] = numDistinct(s.substr(0,j), t.substr(0,m-1)) + numDistinct(s.substr(0,j), t);
        // 就说明此时 “t的前j个元素构成的字符串 在 s的前i个元素构成的字符串 中出现的个数为0”
        dp[n][m] = (dp[n][m] == -1 ? 0 : dp[n][m]);
        return dp[n][m];
    }

    void dfs(const string& s, const string& t, int sIndex, vector<int>& path, vector<vector<int>>& res)
    {
        if (path.size() == t.length())
        {
            res.emplace_back(path);
            return;
        }

        if (sIndex == s.length())
            return;

        for (int i=sIndex;i<s.size();++i)
        {
            if (s[i] == t[path.size()])
            {
                path.push_back(i);
                dfs(s,t,i+1,path,res);
                path.pop_back();
            }
        }
    }

    void dfs2(vector<vector<int>>& cnt, const string& t, vector<int>& path, vector<vector<int>>& res)
    {
        if (path.size() == t.length())
        {
            res.emplace_back(path);
            return;
        }
        
        for (auto& item : cnt[t[path.size()]-'A'])
        {
            if (path.empty())
            {
                path.push_back(item);
                dfs2(cnt,t,path,res);
                path.pop_back(); 
            }
            else
            {
                if (item <= path.back())
                    continue;
                path.push_back(item);
                dfs2(cnt,t,path,res);
                path.pop_back();                     
            }           
        }
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值