题目大意:给定字符串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. 一次mysql主从加keepalived配置搭建及切换演示

    [需求] 根据需求需要搭建mysql主从架构数据库及加keepalived进行自动切换VIP [环境介绍]  系统环境:CentOS release 6.4 (Final) + Server vers ...

  2. #2 numpy pandas初步学习记录

    对numpy中的array进行了了解,array方法的取值arr_2d[0:2, 0:2] pandas 1,read_CSV方法 2,head方法 3,loc方法,取值前开后开, 4,replace ...

  3. XGBoost参数调优完全指南

    简介 如果你的预测模型表现得有些不尽如人意,那就用XGBoost吧.XGBoost算法现在已经成为很多数据工程师的重要武器.它是一种十分精致的算法,可以处理各种不规则的数据.构造一个使用XGBoost ...

  4. C# 创建Web项目时 可以选择的类型在不同VS版本下的对比

    上面这个界面应该是 vs2010的 一. VS2012 .VS2013 其实每个模板的意思,在右边已经显示出来了.Empty,就是一个空的模板,创建后里面除了一个web.config外什么都没有:We ...

  5. TP5架构下链接SQL数据库的一种方法

    1.database设置 2.连接到所需要的表格 *.数据库目录

  6. k8s部署etcd数据库集群

    ⒈下载 https://github.com/etcd-io/etcd/releases ⒉解压 tar -zxvf etcd-v3.3.12-linux-amd64.tar.gz ⒊移动可执行文件及 ...

  7. docker中的镜像中运行Django项目

    首先要在镜像中 安装python3 以及 django2.0.4 目前我用的是这两个版本. 进入镜像 创建项目 进入项目中修改setting文件 将引号和星号添加进括号中 ALLOWED_HOSTS ...

  8. 【medium】78. Subsets

    求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...

  9. selenium启动报错“ incorrect JSON status mapping for 'unknown error' (500 expected)”

    前面讲了工程启动报错“selenium启动报错Unable to read VR Path Registry from C:\Users\clinva\AppData\Local\openvr\ope ...

  10. excel打开csv文件乱码解决办法

    参考链接: https://jingyan.baidu.com/article/4dc408484776fbc8d846f168.html 问题:用 Excel 打开 csv 文件,确认有乱码的问题. ...