Leetcode 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"
Return 3.
题目的意思是从源字符串S中删除一些字符后的字符串与T相等的个数,如果熟悉《算法导论》的话,可以想到动态规划那一节关于由一个字符串通过增删变成另一个字符串解法
本题利用动态规划求解
设决策变量为path[i][j],表示S[1..i]的前i个字符通过删除字符变成T[1..j]的个数
那么状态转移方程为
path[i-1][j-1] (S[i] == T[j])
path[i][j] = path[i-1][j](删除S的第i个字符)+ (不删除字符)
0 (S[i] != T[j] )
class Solution {
public:
int numDistinct(string S, string T) {
int n = S.length(), m=T.length();
if(n <= m) return S==T;
vector<vector<int> > path(n+,vector<int>(m+,));
for(int i = ; i <=n ; ++ i) path[i][] = ;
for(int j =; j <= m; ++ j){
for(int i = ; i <= n ; ++ i){
path[i][j] = path[i-][j] + (S[i-]==T[j-] ? path[i-][j-] : );
}
}
return path[n][m];
}
};
Leetcode Distinct Subsequences的更多相关文章
- 子序列 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 [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 [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [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 ...
随机推荐
- 深入理解javascript原型和闭包(10)——this
接着上一节讲的话,应该轮到“执行上下文栈”了,但是这里不得不插入一节,把this说一下.因为this很重要,js的面试题如果不出几个与this有关的,那出题者都不合格. 其实,this的取值,分四种情 ...
- IOS热更新-JSPatch实现原理+Patch现场恢复
关于HotfixPatch 在IOS开发领域,由于Apple严格的审核标准和低效率,IOS应用的发版速度极慢,稍微大型的app发版基本上都在一个月以上,所以代码热更新(HotfixPatch)对于IO ...
- hashicorp/consul
https://github.com/hashicorp/consul/tree/master/vendor/github.com/boltdb/bolt
- 墙裂推荐一本案例驱动的PhoneGap入门书,早看早收货
清华大学出版社推出的<构建跨平台APP:PhoneGap移动应用实战> 零门槛学APP开发 从无到有 循序渐进 20余个示例APP 3个项目APP 全平台à跨终端à移动开发 完美生命周期: ...
- 【bzoj1076】[SCOI2008]奖励关
题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...
- PHP计算一年有多少周,每周开始日期和结束日期
一年有多个周,每周的开始日期和结束日期 参考代码一:[正在使用的版本] <?php header("Content-type:text/html;charset=utf-8" ...
- Linux下查看操作系统信息、内存情况及cpu信息:cpu个数、核心数、线程数
1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...
- 安卓TabHost页面
<?xml version="1.0" encoding="UTF-8"?> <!-- TabHost组件id值不可变--> <T ...
- androi手机解锁引导程序
1.重启手机进入fastboot模式 一般关机状态下按手机音量减+开机键,成功后会显示fastboot字提示. 2.查看设备信息 fastboot devices 说明:fastboot是一个工具软 ...
- linux c 笔记-4 工程项目阅读推荐
作者:周子涵链接:https://www.zhihu.com/question/27705862/answer/37738315来源:知乎著作权归作者所有,转载请联系作者获得授权. 转自网上不知道什么 ...