[LeetCode] Distinct Subsequences [29]
题目
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"
Return 3
.
解题思路
给两个字符串S和T, 求在字符串S中删除某些字符后得到T。问一共能有多少种删除方法?
这个题用常规方法超时。
得用动态规划,动态规划最基本的就是要找到动态规划方程。
首先记:dp[i][j] 为 从S[0..j-1]中删除某些字符后得 T[0...i-1]的不同删除方法数量。
动态规划方程为: dp[i][j] = dp[i][j-1] + (S[j-1]==T[i-1] ? dp[i-1][j-1] : 0);
代码实现
class Solution {
public:
int numDistinct(string S, string T) {
int m = S.size();
int n = T.size();
if(m<n) return 0;
vector<vector<int> > dp(n+1, vector<int>(m+1, 0));
for(int i=0; i<m; ++i)
dp[0][i] = 1;
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
dp[i][j] = dp[i][j-1] + (S[j-1]==T[i-1] ? dp[i-1][j-1] : 0);
}
}
return dp[n][m];
}
};
对于辅助数组我们其有用到的也仅仅有当前行和其前一行。所以我们能够仅仅申请两行的辅助数组。优化代码例如以下:
class Solution {
public:
int numDistinct(string S, string T) {
int m = S.size();
int n = T.size();
if(m<n) return 0;
vector<int> dp1(m+1, 1);
vector<int> dp2(m+1, 0);
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
dp2[j] = dp2[j-1] + (S[j-1]==T[i-1] ? dp1[j-1] : 0);
}
dp1.clear();
dp1 = dp2;
dp2[0] = 0;
}
return dp1[m];
}
};
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Distinct Subsequences [29]的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- 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: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences 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 OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- CentOS日志的简单介绍
在CentOS7中,系统的日志消息由两个服务负责处理:system-journald和rsyslog. (1).常见的日志及作用 /var/log目录里存放了一些特定于系统和服务的日志文件,由rsys ...
- 理解事件(Event)
Overview 在前几章,我们已经对委托有了一个完整的了解了,本章将会对事件进行一下介绍: 相对于委托,事件再是我们更加频繁的接触的,比如 鼠标的click 事件,键盘的 keydown 事件等等. ...
- sql分组排序取top
写法1: use anypay; select tr.* from (select task_code, max(created_at) as cal from task_log group by t ...
- pkuwc2019自闭记
窝自闭了... 所以这篇\(blog\)咕咕咕了.
- [Arc058E] Iroha and Haiku
[Arc058E] Iroha and Haiku 题目大意 问有多少\(n\)个数的正整数序列,每个数在\([1,10]\)之间,满足存在\(x,y,z,w\)使得\(x\to y-1,y\to z ...
- 【推导】zoj3846 GCD Reduce
题意:给你n个正整数a1...an,一次操作是选择任意两个数ai,aj,将它们都替换成gcd(ai,aj).让你在5n步内将所有数变为1.或者输出不可能. 如果所有数的gcd不为1,显然不可能. 否则 ...
- 属性通知之ObservableCollection
单个属性是如何去通知,在上一章已经介绍过了,那么集合如何做到属性通知呢?这里要介绍ObservableCollection<T>,字面意思就是用于观察的集合. msdn上给出的定义是:表示 ...
- [POI2015]Odwiedziny
[POI2015]Odwiedziny 题目大意: 一棵\(n(n\le5\times10^4)\)个点的树,\(n\)次询问从一个点到另一个点的路径上,每次跳\(k\)个点,所经过的点权和. 思路: ...
- java多线程技术之(callable和future)
接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. Callable接口类似于Runnable,从名字就可以看出来了,但是Runnab ...
- bzoj 3931 最短路+最大流
较水,但因为范围问题WA了两次.... /************************************************************** Problem: 3931 Us ...