题目大意:给定字符串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. PMP知识点(一)——风险登记册

    一.Reference: [管理心得之四十八]<风险登记册>本身的风险 问题日志与风险登记册的区别与联系 PMBOK重要概念梳理之二十六 风险登记册 风险登记单-MBAlib 二.Atta ...

  2. CentOS 7 yum 安装php5.6

    注意--enablerepo=remi --enablerepo=remi-php56这两个参数,指定源的意思 配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh h ...

  3. Python-Struct

    从一个例子开始: >>> from struct import * >>> pack('hhl',1655, 255, 370) b'w\x06\xff\x00r\ ...

  4. java操作mongodb & springboot整合mongodb

    简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...

  5. stylus 实践

    音乐分享: Broken Back - <Halcyon Birds> —————————————————————————————————————————————————————————— ...

  6. Centos系统FastDFS搭建与排错

    FastDFS中Tracker server主要是负载均衡和调度,Storage server主要是文件存储. 1.1 系统环境 [root@ centos fastdfs]# cat /etc/re ...

  7. ubuntu常用命令备忘

    1.把一个目录的文件拷贝另一个文件夹 sudo cp -p /home/likewei/lib/needlib/* /home/likewei/lib/11

  8. day20包

    https://www.cnblogs.com/Eva-J/articles/7292109.html 一.模块: 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名 ...

  9. VisualVM远程监控Java

    1.服务器启动jstatd服务设置: jstatd将使用RMISecurityPolicy,并且需要指定安全策略文件. 安全策略文件必须符合安全策略语法,通过命令 jstatd -J-Djava.se ...

  10. Python+Flask搭建mock api server

    Python+Flask搭建mock api server 前言: 近期由于工作需要,需要一个Mock Server调用接口直接返回API结果: 假如可以先通过接口文档的定义,自己模拟出服务器返回结果 ...