动态规划之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 ...
随机推荐
- Spark_JGroupByKey
package core.java; import java.util.Arrays; import java.util.List; import org.apache.spark.SparkConf ...
- [3]传奇3服务器源码分析一 DBServer
留存 服务端下载地址: 点击这里
- django 网站的搭建(1)
使用 python django 模块来搭建自己的博客网站. 本人环境:阿里云centos7+django1.10+python3.5 使用工具:putty + winscp 1.首先安装python ...
- pyinstaller将python脚本生成exe
一.下载pyinstaller 二.生成exe 下载pyinstaller 1.在C:\python27\Scripts目录下打开cmd界面,执行命令:pip install PyInstaller ...
- HDU 1014 Uniform Generator(题解)
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 5126 cdq+Treap+BIT
这题说的是给了三维空间然后操作 寻求在 x1,y1,z1 x2, y2, z2; (x1<x2, y1<y2,z1<z2) 计算出在 以这两个端点为右下和左上端点的方体内的点的 ...
- 【转】HTTP429
转载:http://codewa.com/question/45600.html Q:How to avoid HTTP error 429 (Too Many Requests) python Q: ...
- Solid Dominoes Tilings (轮廓线dp打表 + 容器)
第一步先打一个表,就是利用轮廓线DP去打一个没有管有没有分界线组合数量的表 #include<bits/stdc++.h> using namespace std; ; <<; ...
- Shell 脚本编程基础
通过本文记录学习Linux Shell的一些笔记思考和总结,以加强记忆和理解.主要学习参考资料有: 1.<鸟哥的Linux私房菜-基础篇>第四版 2.菜鸟教程——Linux教程 3.ear ...
- FTP搭建 共享上网 穿透内网外网
1.ftp原理介绍 FTP只通过TCP连接,没有用于FTP的UDP组件.FTP不同于其他服务的是它使用了两个端口, 一个数据端口和一个命令端口(或称为控制端口).通常21端口是命令端口,20端口是数据 ...