动态规划——Distinct Subsequences
Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:
(The caret symbol ^ means the chosen letters)
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
(The caret symbol ^ means the chosen letters)
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
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的更多相关文章
- 动态规划-Distinct Subsequences
2020-01-03 13:29:04 问题描述: 问题求解: 经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的. 有尝试过dfs来做,但是由于时间复杂度是指数级别的 ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- Distinct Subsequences ——动态规划
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode Distinct Subsequences
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 T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- 一次mysql主从加keepalived配置搭建及切换演示
[需求] 根据需求需要搭建mysql主从架构数据库及加keepalived进行自动切换VIP [环境介绍] 系统环境:CentOS release 6.4 (Final) + Server vers ...
- #2 numpy pandas初步学习记录
对numpy中的array进行了了解,array方法的取值arr_2d[0:2, 0:2] pandas 1,read_CSV方法 2,head方法 3,loc方法,取值前开后开, 4,replace ...
- XGBoost参数调优完全指南
简介 如果你的预测模型表现得有些不尽如人意,那就用XGBoost吧.XGBoost算法现在已经成为很多数据工程师的重要武器.它是一种十分精致的算法,可以处理各种不规则的数据.构造一个使用XGBoost ...
- C# 创建Web项目时 可以选择的类型在不同VS版本下的对比
上面这个界面应该是 vs2010的 一. VS2012 .VS2013 其实每个模板的意思,在右边已经显示出来了.Empty,就是一个空的模板,创建后里面除了一个web.config外什么都没有:We ...
- TP5架构下链接SQL数据库的一种方法
1.database设置 2.连接到所需要的表格 *.数据库目录
- k8s部署etcd数据库集群
⒈下载 https://github.com/etcd-io/etcd/releases ⒉解压 tar -zxvf etcd-v3.3.12-linux-amd64.tar.gz ⒊移动可执行文件及 ...
- docker中的镜像中运行Django项目
首先要在镜像中 安装python3 以及 django2.0.4 目前我用的是这两个版本. 进入镜像 创建项目 进入项目中修改setting文件 将引号和星号添加进括号中 ALLOWED_HOSTS ...
- 【medium】78. Subsets
求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...
- 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 ...
- excel打开csv文件乱码解决办法
参考链接: https://jingyan.baidu.com/article/4dc408484776fbc8d846f168.html 问题:用 Excel 打开 csv 文件,确认有乱码的问题. ...