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

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

代码如下所示:

public int numDistinct(String S, String T) {
int[][] dp = new int[S.length() + 1][T.length() + 1];
dp[0][0] = 1;//initial for(int j = 1; j <= T.length(); j++)//S is empty
dp[0][j] = 0; for (int i = 1; i <= S.length(); i++)//T is empty
dp[i][0] = 1; for (int i = 1; i <= S.length(); i++) {
for (int j = 1; j <= T.length(); j++) {
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];
}
} return dp[S.length()][T.length()];
}

动态规划之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. unity3d生命周期

  2. C++二进制字符串转Base64字符串 Base64字符串转二进制字符串

    封装成类的 . base64格式的字符串,只包含大小写字母.零到九,以及 + / //___base_64.h /*base_64.h文件*/ #ifndef BASE_64_H #define BA ...

  3. 解决ConfigParser配置option的大小写问题

    通常情况下,我们会这样解析配置文件: confiig = ConfigParser.ConfigParser() config.read('xxx.ini') #这个read表示对某个文件用读打开, ...

  4. Unity shader学习之折射

    shader如下: Shader "Custom/Refraction" { Properties { _Cubemap("Cubemap", Cube) = ...

  5. 如何让多个dz论坛共用一个用户数据库

    用户数据库在论坛中是可以独立备份的,备份方法:论坛后台——站长——数据库,备份所有ucenter数据表,也就是用户数据.其他DZ论坛搭建完成以后,可以上传用户数据库,将备份文件使用上传至网站所使用的主 ...

  6. 擠出線寬(Extrusion width),要怎麼設定?

    擠出線寬(Extrusion width),要怎麼設定? Slic3r的作者,把這邊的%設定,跟"層高"做連結.我個人認為擠出線寬,要以噴頭孔徑當做設定參考才好.層高應該只要設定成 ...

  7. Yii restful api跨域

    问题:NO 'Access-Control_Allow-Origin' header is present on the requested resource. 解决方案 <?php names ...

  8. hdu4784

    题意: 给了一个图 从1号节点走到N号节点,然后,每个地方有买卖盐的差价,然后求 到达N的最大价值,一旦走到N这个点就不能再走了,或者走到不能再别的世界走1和N这两个点,然后接下来 用一个 四维的数组 ...

  9. [转]Hive开发经验问答式总结

    本文转载自:http://www.crazyant.net/1625.html 本文是自己开发Hive经验的总结,希望对大家有所帮助,有问题请留言交流. Hive开发经验思维导图 Hive开发经验总结 ...

  10. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...