动态规划——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 ...
随机推荐
- ArcGis使用字段别名Alias Name导出Excel
在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcToolbox工具导出Excel. 工具位置ConversionTools——Excel——Tabl ...
- 《Java》第六周学习总结
本周我学习了第七章和第十章的内容 包括:内部类,匿名类,异常类,断言的使用 File类,文件字节输入输出流,缓冲流,数据流,对象流,scanner的使用扩展还有文件锁等内容令我印象深刻 问题: 在编译 ...
- 虚拟云主机创建多个站点方法(.htaccess实现)
阿里的普通版虚拟云主机最多只能建一个站点,但可以绑定多个域名.如果我们想创建2个或3个主机怎么办呢?难道需要再另外购买一台主机? 其实我们可以通过.htaccess文件来定义相关域名绑定对应的网站目录 ...
- Mybatis的缓存
1.缓存是什么 在 Mybatis 里面,所谓的缓存就是将已经查询过的记录放在内存的缓冲区或文件上,这样如果再次查询,可以通过配置的策略,命中已经查询过的记录,从而提高查询的效率. Mybatis 的 ...
- Thrax-构建基于语法的语言模型工具
安装 http://www.openfst.org/twiki/bin/view/GRM/ThraxQuickTour http://cslu.ogi.edu/~sproatr/Courses/Tex ...
- 在可编辑div的光标下插入html
function pasteHtmlAtCaret(html, selectPastedContent) {//参数1为要插入的html //参数2为boolean 是否选中插入的html 默认为fa ...
- 阿里云服务器+ftp文件操作+基于Centos7的vsftpd配置
路径问题:一定要注意此位置是否需要加入"/" 文件上传方式:被动模式 vsftp完整配置: # # The default compiled in settings are fai ...
- 2018-2019-2 20165221 【网络对抗技术】-- Exp6 信息搜集与漏洞扫描
2018-2019-2 20165221 [网络对抗技术]-- Exp6 信息搜集与漏洞扫描 目录 1. 实践目标 2. 实践内容 3. 各种搜索技巧的应用 a. 搜索网址的目录结构 b.使用IP路由 ...
- day06 字典、元组、set的方法及常用操作
今日内容: 1.深浅拷贝 2.元组 3.字典 4.set 1.深浅拷贝 # 1.值拷贝 # 采用赋值的方法进行 # 只会将堆区容器变量与栈区的绑定关系进行复制 # 2.浅拷贝 # 会将堆区与栈区的绑定 ...
- [NOIP2017提高组]小凯的疑惑-扩展欧几里得
#include<bits/stdc++.h> using namespace std; long long a,b,x,y,ans,tmp; inline void ex_gcd(lon ...