动态规划——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 ...
随机推荐
- 如何运用jieba库分词
使用jieba库分词 一.什么是jieba库 1.jieba库概述 jieba是优秀的中文分词第三方库,中文文本需要通过分词获得单个词语. 2.jieba库的使用:(jieba库支持3种分词模式) 通 ...
- Flsk-Bootstrap-2
目录 Flsk-Bootstrap-2 结构 解压Bootstrap 制作基础模板 视图函数 初始文件 启动文件 浏览器 Flsk-Bootstrap-2 参考:Flask 项目中使用 bootstr ...
- 20175306 MyCP博客总结
课后必做题:MyCP总结 cp命令了解: · 作用:cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同 ...
- # 20175333曹雅坤《Java程序设计》第2周学习总结
教材学习内容总结 1.学习第二,三章ppt,并观看视频. 2.在虚拟机中连接到码云,克隆代码,编译与运行教材上的例子. 3.在虚拟机上安装相关配置,使其满足学习要求. 4.运行并截图上传监督学习脚本s ...
- Shell中字符串的切割、拼接、比较、替换
[截取] 一.Linux shell 截取字符变量的前8位,有方法如下: expr substr “$a” 1 8 : 二.按指定的字符串截取 第一种方法: ${varible##*string} # ...
- Spring Cloud微服务集成配置中心
1. 搭建Spring Cloud Config配置中心(见上一篇博客) 2. 创建微服务项目bounter-simon-app,pom文件如下: <?xml version="1.0 ...
- JavaScript 变量声明:var、let、const
1. 概述 1.1 说明 在ES5 声明变量的方法:var命令和function命令. 在ES6 声明变量的方法:var命令.function命令.let命令.const命令.import命令.cla ...
- LeetCode 9. Palindrome Number(c语言版)
题目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- Git客户端(TortoiseGit)基本使用详解
1. 环境安装 Git最新版下载地址:https://gitforwindows.org/ TortoiseGit,Git客户端,32/64位最新版及对应的语言包下载地址:https://tortoi ...
- 十 LVS 负载均衡
回顾nginx 反向代理负载均衡 负载均衡的妙用 负载均衡(Load Balance)集群提供了一种廉价.有效.透明的方法, 来扩展网络设备和服务器的负载.带宽.增加吞吐量.加强网络数据处理能力. 提 ...