动态规划-Distinct Subsequences
2020-01-03 13:29:04
问题描述:

问题求解:
经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的。
有尝试过dfs来做,但是由于时间复杂度是指数级别的,所以会TLE。
public int numDistinct(String s, String t) {
int n1 = s.length();
int n2 = t.length();
int[][] dp = new int[n2 + 1][n1 + 1];
for (int i = 0; i <= n1; i++) dp[0][i] = 1;
for (int i = 1; i <= n2; i++) {
for (int j = 1; j <= n1; j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
else dp[i][j] = dp[i][j - 1];
}
}
return dp[n2][n1];
}
动态规划-Distinct Subsequences的更多相关文章
- 动态规划——Distinct Subsequences
题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...
- 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 ...
随机推荐
- 推拿O2O 想说爱你还不容易
想说爱你还不容易" title="推拿O2O 想说爱你还不容易"> <屌丝男士>第四季最后一集里,乔杉终于圆了"大保健"的梦想,可惜 ...
- 纯css3配合vue实现微信语音播放效果
前言 每次写点东西都扯两句-0-,这几天一半精力放在移动端,一半维护之前的项目.书也少看了,不过还好依旧保持一颗学习的心.对于css3我是之前有专门整理过的,因此对于原理之前也算了解.今天是项目中遇到 ...
- jquery.qrcode笔记
由于一个坑爹的项目需要生成二维码扫描,后台由于数据库比较麻烦,就只能前端做了,于是乎找到Js生成qrcode的一个库:https://github.com/jeromeetienne/jquery-q ...
- 正则表达式之RegExp对象
1.定义 RegExp对象是原生JavaScript中表示正则表达式的对象:是正则表达式(regular expression)的简写.有一点需要明确知道,正则表达式只能对字符串进行操作. 2.语法 ...
- 使用Python生成自己的特色二维码
二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据 ...
- 7-19 计算有n个字符串中最长的字符串长度 (40 分)
编写程序,用于计算有n(1<n<10)个字符串中最长的字符串的长度.前导空格不要计算在内! 输入格式: 在第一行中输入n,接下的每行输入一个字符串 输出格式: 在一行中输出最长的字符串的长 ...
- localstorage浏览器储存
需求 a.html页面生成订单信息,b.html中调用. 通过不操作数据库,直接在浏览器自带的数据库中进行操作,当然主要是对Json数据的操作. a.html代码部分: <!--html--&g ...
- CentOS下安装Anaconda和pycharm
前情提要:Linux越来越受大家喜爱,而在Linux中有一个社区很活跃的系统:那就是CentOS:而Anaconda又是几乎就一劳永逸的,你装了它之后基本上很多类库就不用再装了.然后就是pycharm ...
- 报错: raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
Django2.0同步Mysql数据库时出现的问题 执行 python manage.py makemigrations 报错 # 报错位置 File "G:\python\lib\site ...
- 本地目录配置多个远程Git仓库
目录 情景一:不同的库分别 pull/push 1. 使用git命令配置 2. 修改.git/config 文件 3. 操作 情景二:不同的库一次push 1. 使用git命令配置 2. 修改.git ...