Distinct Subsequences
https://leetcode.com/problems/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
.
定义f[i][j]表示在S[0,i]中,T[0,j]出现了几次。无论s[i]和t[j]是否相等,如果不匹配s[i],则f[i][j]=f[i-1][j];若s[i]==s[j],
f[i][j]=f[i-1][j]+[i-1][j-1]。
另外,当t=""时,只有一种匹配方式,f[i][0]=1;当s="",t!=""时,无论如何无法匹配,此时f[0][j]=0。
参考:http://www.cnblogs.com/yuzhangcmu/p/4196373.html
int numDistinct(string s, string t) {
int m=s.size();
int n=t.size(); vector<vector<int>> f(m+,vector<int>(n+,)); for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(i== && j==)
f[i][j]=;
else if(i==)
f[i][j]=;
else if(j==)
f[i][j]=;
else
f[i][j]=f[i-][j]+(s[i-]==t[j-]?f[i-][j-]:);
}
}
return f[m][n];
}
Distinct Subsequences的更多相关文章
- [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 ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
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 ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- ASP.NET WEB API 帮助文档引用单独项目中的DTO,见面上不显示字段注释问题解决办法
StackOverFlow上的解决办法: 问题地址
- 一个脚本可以一直运行 ignore_user_abort
php中ignore_user_abort函数的用法 PHP中的ignore_user_abort函数是当用户关掉终端后脚本不停止仍然在执行,可以用它来实现计划任务与持续进程,下面会通过实例讨论ign ...
- 分享10条PHP性能优化的小技巧,帮助你更好的用PHP开发:
1. foreach效率更高,尽量用foreach代替while和for循环. 2. 循环内部不要声明变量,尤其是对象这样的变量. 3. 在多重嵌套循环中,如有可能,应当将最长的循环放在内层,最短循环 ...
- WPF学习系列 绘制旋转的立方体
我是一年经验的web程序员,想学习一下wpf,比较喜欢做项目来学习,所以在网上找了一些项目,分析代码,尽量能够做到自己重新敲出来 第一个项目是 中间的方块会不停的旋转. 第一步,新建wpf项目 第二步 ...
- Redis查询当前库有多少个 key
info可以看到所有库的key数量 dbsize则是当前库key的数量 keys *这种数据量小还可以,大的时候可以直接搞死生产环境. dbsize和keys *统计的key数可能是不一样的,如果没记 ...
- mongodb未授权访问漏洞
catalogue . mongodb安装 . 未授权访问漏洞 . 漏洞修复及加固 . 自动化检测点 1. mongodb安装 apt-get install mongodb 0x1: 创建数据库目录 ...
- 利用django创建一个投票网站(四)
创建你的第一个 Django 项目, 第四部分 这一篇从第三部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,专注于简单的表单处理并且精简我们的代码. 编写一个简单的表单 让我们更新一下在上一个 ...
- [教程] [授权翻译] 使用补丁修改DSDT/SSDT [DSDT/SSDT综合教程]
[教程] [授权翻译] 使用补丁修改DSDT/SSDT [DSDT/SSDT综合教程] http://bbs.pcbeta.com/viewthread-1571455-1-1.html [教程] [ ...
- python之生成器
def repeater(value): while True: new = yield value print(first, new) if new is not None: value = new ...
- 纯CSS3实现动态导航栏目
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...