[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, 求 T 在 S 中有多少个不同的子序列。
一开始看到这道题,没有思路,在网上看了别人的解答才了解到如何解题。这是一道 dp 题,如果能想得到状态转义 关系,答案就比较明显了。解法有点想 背包问题的解法。
二维数组 vv[T.len][S.len] 存储中间结果。 vv[i][k], 表示 T[0, i] 在 S[0, k] 中的不同的子序列个数。
状态转换关系,文字描述:
当 S[i] 和 T[k] 不相等时, T[0, i] 在 S[0, k] 的不同子序列个数,和在S[0, k-1] 的不同子序列个数相同。
当 S[i] 和 T[k] 相同时, T[0, i] 在 S[0, k] 的不同子序列个数等于 S[i] 被采取的情况( vv[i-1][k-1] ), 加上 S[i] 不被采取的情况 ( vv[i][k-1] )。
状态转换关系,公式表示:
当 S[i] != T[k] 时,vv[i][k] = vv[i][k-1]
当 S[i] == T[k] 时,vv[i][k] = vv[i-1][k-1] + vv[i][k-1]
int numDistinct(string s, string t) { if (t.size() > s.size()) {
return ;
} vector<vector<int>> vv(t.size(), vector<int>(s.size(), -)); // 边界值处理
for (int i = ; i < vv.size(); i++) {
vv[i][i-] = ;
} // 边界值处理
if (s[] == t[]) {
vv[][] = ;
}else{
vv[][] = ;
} // 边界值处理
for (int i = ; i < vv[].size(); i++) {
if (t[] == s[i]) {
vv[][i] = vv[][i-] + ;
}else{
vv[][i] = vv[][i-];
}
} // 状态转移
for (int i = ; i < vv.size(); i++) {
for (int k = i ; k < vv[i].size(); k++) {
if (t[i] != s[k]) {
vv[i][k] = vv[i][k-];
}else{
vv[i][k] = vv[i-][k-] + vv[i][k-];
}
}
} return vv[t.size()-][s.size()-];
}
[LeetCode] Distinct Subsequences 解题思路的更多相关文章
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- [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] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
随机推荐
- JS 頁面實時更新時間
function startTime() { var today = new Date(); //定义日期对象 var yyyy = today.getFullYear(); //通过日期对象的get ...
- Oracle学习第三讲
关联查询 笛卡尔积 指做关联操作的每个表的每一行都和其他表的每一行组合,假设两个表的记录条数分别为x和y,笛卡尔积将返回x*y条记录 例如:select count(*) from emp; sele ...
- ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...
- Google HTML/CSS/JS代码风格指南
JS版本参见:http://www.zhangxinxu.com/wordpress/2012/07/google-html-css-javascript-style-guides/ HTML/CSS ...
- oracle简单两个操作
sqlplus sys/密码 as sysdba ALTER USER 账号 IDENTIFIED BY 新密码; select * from (select rownum 别名 ,表名.* fro ...
- jQuery实现的分页功能,包括ajax请求,后台数据,有完整demo
一:需求分析 1)需要首页,末页功能 2)有点击查看上一页,下一页功能 3)页码到当前可视页码最后一页刷新页面 二:功能实现思路 也是分为三部分处理 1)点击首页,末页直接显示第一页或者最后一页内容, ...
- 帝国cms 灵动标签调用顶级栏目导航
[e:loop={"select classname,classpath from [!db.pre!]enewsclass where bclassid=0 order by classi ...
- git+Coding.netの小试牛刀
一.将本地项目推送到Coding中 1.在Coding中新建项目,填写项目名称和项目描述,设置属性,勾选初始化仓库
- jQuery Mobile 控制 select 的显示隐藏 display none
如需要动态控制下拉选择菜单select的显隐,一般考虑使用display:none这个方法. 但在jQueryMobile中的select添加自定义的css,display:none 是无效的. 解决 ...
- Displaying 1-16 of 86 results for: deep learning
Displaying 1-16 of 86 results for: deep learning Deep Learning By Adam Gibson, Josh Patterson Publis ...