【Leetcode】115. Distinct Subsequences
Description:
Given two string S and T, you need to count the number of T's subsequences appeared in S. The fucking problem description is so confusing.
Input:
String s and t
output:
The number
Analysis:
It's a dynamic processing problem. I drew the dynamic processing of counting the seq numbers and then got the correct formula by guessing? :) Most times I work out the final formula by deducing! Then i back to think it's real sense in the problem.
dp[i][j] represents the number of subsequences in string T (ending before index i) are appeared in string S (ending before index j). So, dp can be processed by the follow formula:
= dp[i][j-1] + dp[i-1][j-1] if s[j] == t[i]
dp[i][j]
= dp[i][j-1] if s[j] != t[i]
BYT:
The fucking input size of test cases in serve are ambiguity! So if you create a 2-dimension array in defined size, you will be in trouble. Dynamic structures like vector will be better!
Code:
class Solution {
public:
int numDistinct(string s, string t) {
if(s.length() == || t.length() == ) return ;
//int dp[50][10908];
vector<vector<int>> dp(t.length() + , vector<int>(s.length() + , ));
dp[][] = (t[] == s[])?:;
for(int i = ; i < s.length(); i ++){
if(s[i] == t[]) dp[][i] = dp[][i - ] + ;
else dp[][i] = dp[][i - ];
}
for(int i = ; i < t.length(); i ++){
dp[i][i - ] = ;
for(int j = i; j < s.length(); j ++){
dp[i][j] = (t[i] == s[j])? (dp[i][j - ] + dp[i - ][j - ]):dp[i][j - ];
}
}
return dp[t.length() - ][s.length() - ];
}
};
【Leetcode】115. Distinct Subsequences的更多相关文章
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Lintcode】118.Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【LeetCode】491. Increasing Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
随机推荐
- BZOJ 4819 Luogu P3705 [SDOI2017]新生舞会 (最大费用最大流、二分、分数规划)
现在怎么做的题都这么水了.. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=4819 (luogu) https://ww ...
- RequestMapping_HiddenHttpMethodFilter 过滤器
[REST] 1.REST:即Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以得 ...
- 【Codeforces 242C】King's Path
[链接] 我是链接,点我呀:) [题意] 让你找到(x0,y0)到(x1,y1)的一条最短路 走过的点必须在所给的n个横向路径上 [题解] 因为n条横向路径上的点最多不会超过10的5次方个,所以我们可 ...
- HDU 1249 三角形的分割
可以将三角形的三条边一条一条加进图形中观察 假设添加第n个三角形 前n-1个三角形将区域划分为sum[n-1] 第n个三角形每条边最多能经过前n-1个三角形每条三角形的两条边 , 一条边切完增加了 2 ...
- Prime Land(poj 1365)
题意:这题题意难懂,看了题解才知道的.比如第二组sample,就是5^1*2^1=10, 求10-1即9的质因数分解,从大到小输出,即3^2.本来很简单的嘿,直接最快速幂+暴力最裸的就行了. #inc ...
- Thread的方法join()使用
join()的作用:Waits for this thread to die.等待线程对象销毁.在Thread源码中可以看到join源码是使用了wait()方法来实现等待功能. 因为join()内部使 ...
- 两张图让git新手在项目中运用git命令行
创建分支命令: git branch (branchname) 切换分支命令: git checkout (branchname) 当你切换分支的时候,Git 会用该分支的最后提交的快 ...
- Jenkins+Github持续集成
由于最近团队代码库从coding迁移到github,在CI工具的选型上尝试了travis-ci和circle-ci,最后决定自己搭建CI服务器,而我也有幸认领了这个任务的调研,因此有了这篇文章. 之前 ...
- STL 笔记(五) 算法 algorithm
在 STL 中,算法是一系列的函数模版.STL 提供了大概 70 个算法,由头文件 <algorithm>.<numeric>.<functional>组成. 头文 ...
- 如何定义StrokeIt手势 常用StrokeIt手势大全
1 最小化,最大化,最小化所有(显示桌面) 斜向上表示最大化或者还原,斜向下表示最小化,适用于任务管理器和一般应用程序(有这三个按钮的都可以),先斜向下再斜向上表示显示桌面,这个在WIN7系统中不太实 ...