动态规划之115 Distinct Subsequences
题目链接: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的更多相关文章
- [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 ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 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 ...
- [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 ...
- 115. Distinct Subsequences *HARD* -- 字符串不连续匹配
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- python模拟艺龙网登录requests包
对比urllib.urllib2与requests不难发现,前者功能更强大,但是实现一个功能要写很多的代码,后者,requests代码简洁,用起来更快速 下面一个模拟登录的代码:看看吧一共也没有几行就 ...
- 10.用js下载文件(需要后端链接)
用js下载文件 PS:本文说的,并非如何用js创建流.创建文件.实现下载功能. 而是说的:你已知一个下载文件的后端接口,前端如何请求该接口,实现点击按钮.下载文件到本地.(可以是zip啦. ...
- PHP开启mysqli扩展
Call to undefined function mysqli_connect()解决这个问题需要开启mysqli扩展开启mysqli扩展需要这两个步骤缺一不可1.在php.ini中搜索php_m ...
- rabbitmq坑点与异常处理
from:http://www.cnblogs.com/gossip/p/4573056.html 一.None of the specified endpoints were reachable 这 ...
- mongodb mapredReduce 多个条件分组(group by)
from:https://my.oschina.net/chiyong/blog/289138 Mongodb 没有传统数据库的group函数,如果分组需要走MapReduce.这种MR与Hadoop ...
- 5.无监督学习-DBSCAN聚类算法及应用
DBSCAN方法及应用 1.DBSCAN密度聚类简介 DBSCAN 算法是一种基于密度的聚类算法: 1.聚类的时候不需要预先指定簇的个数 2.最终的簇的个数不确定DBSCAN算法将数据点分为三类: 1 ...
- while练习题
# 1. 使用while循环输出1 2 3 4 5 6 8 9 10count = 1while count <= 10: if count == 7: count += 1 continue ...
- html5-样式表的使用-初步
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 20165215 预备作业3 Linux安装及学习
Linux安装 根据老师的链接,我VirtualBox下载的是5.2.6的版本,下载Ubuntu时使用老师的链接总是出现404 Not found的页面,于是我采用其它方式下载了16.04.3的版本 ...
- CXF框架入门(重点)
l CXF是一个开源的webservice框架 l CXF支持的协议:SOAP.XML/HTTP等 l CXF可以很好的和spring集成 l CXF可以部署到tomcat.jboss.jetty等服 ...