题目链接: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];

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

代码如下所示:

  1. public int numDistinct(String S, String T) {
  2. int[][] dp = new int[S.length() + 1][T.length() + 1];
  3. dp[0][0] = 1;//initial
  4.  
  5. for(int j = 1; j <= T.length(); j++)//S is empty
  6. dp[0][j] = 0;
  7.  
  8. for (int i = 1; i <= S.length(); i++)//T is empty
  9. dp[i][0] = 1;
  10.  
  11. for (int i = 1; i <= S.length(); i++) {
  12. for (int j = 1; j <= T.length(); j++) {
  13. if (S.charAt(i - 1) != T.charAt(j - 1)) {
  14. dp[i][j] = dp[i - 1][j];
  15. }
  16. if (S.charAt(i - 1) == T.charAt(j - 1))
  17. dp[i][j] = dp[i - 1][j]+dp[i - 1][j - 1];
  18. }
  19. }
  20.  
  21. return dp[S.length()][T.length()];
  22. }

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

  1. [LeetCode] 115. Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  2. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  4. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

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

  6. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

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

  8. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  9. 115. Distinct Subsequences *HARD* -- 字符串不连续匹配

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. windows连接远程win服务器失败,win7win10都存在此问题,显示出现身份验证错误,要求的函数不受支持,可能由于CredSSP加密Oracle修正 (原)

    之前电脑windows+mstsc连接另一个windows服务器正常登陆,可在更新本地系统为win10以后,登陆就出现了问题,提示的错误是,出现身份验证错误.要求的函数不受支持,可能由于CredSSP ...

  2. D Cloud of Hashtags Codeforces Round #401 (Div. 2)

    Cloud of Hashtags [题目链接]Cloud of Hashtags &题意: 给你一个n,之后给出n个串,这些串的总长度不超过5e5,你要删除最少的单词(并且只能是后缀),使得 ...

  3. MyBatis基础入门《三》Select查询集合

    MyBatis基础入门<三>Select查询集合 描述: 代码新增了一个MybatisUtil工具类,查询数据库返回集合的时候,接收数据的三种方式.由于代码会渐渐增多,未涉及改动过的文件不 ...

  4. 创建 .m2 文件夹

    首次使用 Maven 创建 .m2 文件夹 1. cmd2. mvn help:system

  5. string 常量池 栈 堆

  6. Unicode字符需要几个字节来存储?

    0)学习笔记: 我们常说的这句话“Unicode字符是2个字节”这句话有毛病 Unicode目前规划的总空间有17个平面, 0x0000---0x10FFFF,每个平面有 65536 个码点. Uni ...

  7. python 修改xml文档 ing

    原xml文件 <?xml version="1.0" encoding="utf-8"?> <catalog> <maxid> ...

  8. python 内置函数enumerate()

    enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.在python 3中返回一个生成器,代码如下: a ...

  9. php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小

    php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小 UNIX时间戳转换为日期用函数: date() ,date('Y-m-d H:i:s', 1500219870); 日 ...

  10. Codeforce 791A - Bear and Big Brother

    Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. ...