动态规划之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 ...
随机推荐
- ####### Scripts Summary #######
Scripts Summary Version: 1.0.1 issueDate: 2017-11-11 modifiedDate: 2017-11-28 0.configuration 1.oper ...
- C语言记录汇总
uint32_t 转载自:http://blog.sina.com.cn/s/blog_6aea878e0100tl0f.html体会1>. 在写程序时注意"无符号类型&quo ...
- Linux(Centos)服务器配置node项目
以阿里云服务器,CentOS系统为例 上一节已经提到怎么安装nodejs,以下是以vue项目为例 步骤: (1)首先安装vue脚手架@vue/cli, 官网参考 vue-cli3.x [root@lu ...
- 使用SQL Developer导入文件时出现的一个奇怪的问题
SQL Developer 的版本是 17.3.1.279 当我导入文件的时候,在Data Preview 的阶段,发现无论选择还是取消选择 Header,文件中的第一行总会被当作字段名. 后来在Or ...
- 【转】Loadrunner 性能指标定位系统瓶颈
转至:http://www.51testing.com/html/63/n-1224463.html Loadrunner 性能指标定位系统瓶颈 判断CPU瓶颈 1, %processor time ...
- Java多线程-----Thread常用方法
1.public Thread(Runnable target,String name) 创建一个有名称的线程对象 package com.thread.mothed; public class Th ...
- url组成
- Linux基础命令---文本编辑sed
sed sed是一种流编辑器,用来从输入流中读取内容并完成转换,输入流可以来自一个文件,也可以来自一个管道. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openS ...
- tomcat1章1
package ex01.pyrmont; import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddr ...
- AtCoder Beginner Contest 084(AB)
A - New Year 题目链接:https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit ...