动态规划之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 ...
随机推荐
- javascript获取style兼容性问题
获取css 样式的方法有三种 : style, currentStyle , getComputedStyle style (无兼容性问题) 获取语法: ele.style.attr : 设置语法:e ...
- PHP与JSP简单比较
比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较php和Java的Web开发.以下就几个主要方面进行的比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手 ...
- linux工作目录切换命令
1.pwd命令 pwd命令用于显示用户当前所处的工作目录,格式为“pwd [选项]”. 2.cd命令 cd命令用于切换工作路径,格式为“cd [目录名称]”. 这个命令应该是最常用的一个Linux命令 ...
- c++ string split
#include <iterator> #include <regex> std::vector<std::string> s_split(const std::s ...
- ==与Equals的作用
string str1 = "Blackteeth"; string str2 = str1; string str3 = "Blackteeth"; Cons ...
- keras如何求分类问题中的准确率和召回率
https://www.zhihu.com/question/53294625 由于要用keras做一个多分类的问题,评价标准采用precision,recall,和f1_score:但是keras中 ...
- 【2017-03-28】JS基础、DOM操作
一.JS基础 1.javascript功能 ⑴进行数据运算 ⑵控制浏览器功能 ⑶控制元素的属性.样式.内容 2.javascript位置和格式 可以放在html页的任意位置,也可以创建一个以js结尾的 ...
- STL容器之vector
[1]模板类vector 模板类vector可理解为广义数组.广义数组,即与类型无关的数组,具有与数组相同的所有操作. 那么,你或许要问:既然C++语言本身已提供了一个序列式容器array,为什么还要 ...
- golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题
golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题 今天测试了重新建一个项目生成新的表,然后复制到旧的项目 ...
- 普通程序员转型AI免费教程整合,零基础也可自学
普通程序员转型AI免费教程整合,零基础也可自学 本文告诉通过什么样的顺序进行学习以及在哪儿可以找到他们.可以通过自学的方式掌握机器学习科学家的基础技能,并在论文.工作甚至日常生活中快速应用. 可以先看 ...