动态规划—distinct-subsequences
题目:
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE"is a subsequence of"ABCDE"while"AEC"is not).
Here is an example:
S ="rabbbit", T ="rabbit"
Return3.
思路:
1. 初始化一个矩阵number[i][j]用来记录字符串T的前j个字符出现在字符串S的前i个字符的次数,当j=0时,令number[i][j]=1;
2. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]没有影响,即number[i][j]=number[i-1][j];
3. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]有影响,number[i][j]除了要算上原来的number[i-1][j],还要算上新的可能性,即number[i-1][j-1].
例子
| 0 | r | a | b | b | i | t | |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| r | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
| a | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| b | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| b | 1 | 1 | 1 | 2 | 1 | 0 | 0 |
| b | 1 | 1 | 1 | 3 | 3 | 0 | 0 |
| i | 1 | 1 | 1 | 3 | 3 | 3 | 0 |
| t | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
代码:
public static int result(String str1, String str2){
int len1 = str1.length(), len2 = str2.length();
int[][] res = new int[len1+1][len2+1];
for(int i=0;i<len1+1;i++){
res[i][0] = 1;
}
for(int i=1;i<len1+1;i++){
for(int j=1;j<len2+1;j++){
if(str1.charAt(i-1)!=str2.charAt(j-1))
res[i][j] = res[i-1][j];
else
res[i][j] = res[i-1][j]+res[i-1][j-1];
}
}
return res[len1][len2];
}
动态规划—distinct-subsequences的更多相关文章
- 动态规划——Distinct Subsequences
题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...
- 动态规划-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 ...
随机推荐
- Alexa TOP 100万的域名列表
Alexa是一家专门发布网站世界排名的网站,是亚马逊公司的一家子公司.Alexa每天在网上搜集多达几十亿的网址链接,而且为其中的每一个网站进行了排名. Alexa通过Alexa官网查询好像TOP 50 ...
- 企业级技术解决方案:hbase+es
1:需求: 解决海量数据的存储,并且能够实现海量数据的秒级查询 Hbase是典型的nosql,是一种构建在HDFS之上的分布式.面向列的存储系统,在需要的时候可以进行实时的大规模数据集的读写操作:但是 ...
- android发送udp,tcp消息
发送方创建步骤: 1. 创建一个DatagramSocket对象 DatagramSocket socket = new DatagramSocket (4567); 2. 创建一个 InetA ...
- [转]Vue项目全局配置微信分享思路详解
这篇文章给大家介绍了vue项目全局配置微信分享思路讲解,使用vue作为框架,使用vux作为ui组件库,具体内容详情大家跟随脚本之家小编一起学习吧 这个项目为移动端项目,主要用于接入公众号服务.项目采用 ...
- PJSIP库设置Via地址
好记性不如烂笔头,解决项目问题,调试代码跟踪到PJSIP 设置Via地址位置,记录下来,以备后用. PJSIP库在方法stateless_send_transport_cb中设置Via地址值,该方法在 ...
- 关于问题的四个单词区别: question problem matter issue
[[ 网上讨论的 problem, question, issue, matter这些名词均含"问题"之意.problem: 指客观上存在的.难以处理或难以理解的问题.questi ...
- dependencies 和 starter
以 spring-cloud-alibaba-dependencies-1.5.0.RELEASE 为例: <dependency> <groupId>com.alibaba. ...
- dropna()函数
参数: axis: default 0指行,1为列 how: {‘any’, ‘all’}, default ‘any’指带缺失值的所有行;'all’指清除全是缺失值的 thr ...
- ubuntu 上用virtualenv安装python不同版本的开发环境。
1.用pip安装virtualenv apt-get install python-virtualenv 2.创建python2的虚拟环境,进入要创建虚拟环境的目录下,我是放在/home/pyenv/ ...
- sql exist 和not exist(转载)
exists : 强调的是是否返回结果集,不要求知道返回什么, 比如: select name from student where sex = 'm' and mark exists(select ...