题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数。
Example 1:
Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters)
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^    ^
babgbag
^    ^^
babgbag
  ^  ^^
babgbag
    ^^^
这个题的状态就是原问题的子问题:从S[0..i]中选取字符组成T[0..j]的方案数,有时候越做越有感觉:既然是动态规划问题,求解过程中的状态肯定是原问题的子问题(或者说大多数情况下是这样的)。主要问题在于这个状态转移方程,我拿其中一个状态出来说,在数组下标都不越界的情况下,比如说我现在求dp[i][j],方案数包括上一个位置的匹配情况dp[i-1][j-1]乘以(S[i] == T[j])这个布尔的判断值和当次匹配的前一个情况dp[i-1][j],举个例子来说,T[0..1]为“ba”,则dp[i][0]的方案数一定包括S[0..i-1]匹配T[0]=b时的方案数dp[i-1][0]乘以(S[i] == T[0])这个布尔的判断值和S[0..i-1]匹配T[0..1]="ba"时的方案数dp[i-1][1],加后面那项的原因是dp[i][j]肯定是要包括所有匹配了T[j]的方案数量,这个递推关系是2包括1,3包括2,4包括3...这样的包含关系,所以dp[i][j]只需要再加上一个dp[i-1][j]即可。而前面那个乘以布尔值的原因是如果之前匹配到T[j-1]的话前面那个乘积值dp[i-1][j-1]*(S[i] == T[j])可以不加相当于加0,只为了少个if判断,但是后面的dp[i-1][j]还是要加上的。
 
当然要注意边界条件,我在leetCode上跑程序的时候出现了输入串长度为0的情况导致了数组越界问题。
 
 public int numDistinct(String s, String t) {
int slen = s.length();
int tlen = t.length();
if(slen==0||tlen==0)return 0;
if(s.equals(t))return 1;
int[][]dp = new int [slen][tlen];
for(int i = 0;i<slen;i++)
for(int j = 0;j<tlen;j++)
dp[i][j] = 0;
int temp = 0;
char t0 = t.charAt(0);
for(int i = 0;i<slen;i++) {
if(s.charAt(i)==t0) {
temp++;
dp[i][0] = temp;
}else{
if(i>0)dp[i][0] = dp[i-1][0];
else dp[i][0] = 0;
}
} for(int i = 1;i<tlen;i++) {
for(int j = i;j<slen;j++) {
temp = (s.charAt(j)==t.charAt(i))?1:0;
dp[j][i] = dp[j-1][i]+dp[j-1][i-1]*temp;
}
}
return dp[slen-1][tlen-1];
}
 

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

  1. 动态规划-Distinct Subsequences

    2020-01-03 13:29:04 问题描述: 问题求解: 经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的. 有尝试过dfs来做,但是由于时间复杂度是指数级别的 ...

  2. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  3. Distinct Subsequences ——动态规划

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

  4. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  5. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

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

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

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

  7. Leetcode Distinct Subsequences

    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 T in S. A subsequen ...

  9. [Leetcode][JAVA] Distinct Subsequences

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

随机推荐

  1. javascript 获取字符递增

    比如A->B,AA->AB,Z->AA 参考https://blog.csdn.net/xiaotao2004/article/details/75096607 C#版本,改写为ja ...

  2. [译]Async/Await - Best Practices in Asynchronous Programming

    原文 避免async void async void异步方法只有一个目的:使得event handler异步可行,也就是说async void只能用于event handler. async void ...

  3. AIC与BIC

    首先看几个问题 1.实现参数的稀疏有什么好处? 一个好处是可以简化模型.避免过拟合.因为一个模型中真正重要的参数可能并不多,如果考虑所有的参数作用,会引发过拟合.并且参数少了模型的解释能力会变强. 2 ...

  4. mysql Using filesort 索引不可用问题

        今天上班发现线上机器CPU告警,看了一下发现是mysqld一直占用CPU处于满负荷状态,show processlist;一下,发现很多查询在排序状态,随便拿了一条sql explain看了一 ...

  5. C# - 设计模式 - 模板模式

    模板模式 问题场景 咖啡和茶派生于抽象类饮料,咖啡和茶都具有烧水的方法,所以可以将烧水的方法提取到抽象类饮料中去实现,而咖啡具有一个向杯子加咖啡粉的方法,茶具有一个向杯子加茶叶的方法,看起来两个方法是 ...

  6. python算法&二分查找法

    import random def random_list(n): result = [] ids = list(range(1001,1001+n)) a1 = ["赵",&qu ...

  7. c#串口编程(转)

    在单片机项目开发中,上位机也是一个很重要的部分,主要用于数据显示(波形.温度等).用户控制(LED,继电器等),下位机(单片机)与 上位机之间要进行数据通信的两种方式都是基于串口的: USB转串口 — ...

  8. OSG开源教程(转)

    例:geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); 来指定要利用这些数据生成一个怎么样的形状. ...

  9. PHP Excel使用方法

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...

  10. SecureCRT标签显示IP地址

    当使用SecureCRT连接到linux服务器后,SecureCRT的标签会随着操作目录的改变而改变,当连接多个的时候很不好区分,所以需要设置标签栏固定显示IP地址信息. options->Se ...