【Lintcode】118.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).
Given S = "rabbbit", T = "rabbit", return 3.
题解:
Solution 1 ()
class Solution {
public:
int numDistinct(string &S, string &T) {
int n1 = S.size(), n2 = T.size();
vector<vector<int>> dp(n2 + , vector<int>(n1 + , ));
for (int i = ; i <= n1; ++i) {
dp[][i] = ;
}
for (int i = ; i <= n2; ++i) {
for (int j = ; j <= n1; ++j) {
if (T[i - ] == S[j - ]) {
dp[i][j] = dp[i][j - ] + dp[i - ][j - ];
} else {
dp[i][j] = dp[i][j - ];
}
}
}
return dp[n2][n1];
}
};
Notice that we keep the whole m*n matrix simply for dp[i - 1][j - 1]. So we can simply store that value in a single variable and further optimize the space complexity. The final code is as follows.
Solution 2 () from here
class Solution {
public:
int numDistinct(string s, string t) {
int m = t.length(), n = s.length();
vector<int> cur(m + , );
cur[] = ;
for (int j = ; j <= n; j++) {
int pre = ;
for (int i = ; i <= m; i++) {
int temp = cur[i];
cur[i] = cur[i] + (t[i - ] == s[j - ] ? pre : );
pre = temp;
}
}
return cur[m];
}
};
【Lintcode】118.Distinct Subsequences的更多相关文章
- 【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 ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- 【easy】118.119.杨辉三角
这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...
随机推荐
- Cocostudio学习笔记(5) Text + TextAtlas + TextBMFont
下午一群大学生到我们公司參观学习,搞得我好紧张.于是滔滔不绝的给他们介绍了怎样开发一款游戏... 今晚研究的控件就是三个label:Text,TextAtlas,TextBMFont 我先在cocos ...
- hadoop生态系统学习之路(六)hive的简单使用
一.hive的基本概念与原理 Hive是基于Hadoop之上的数据仓库,能够存储.查询和分析存储在 Hadoop 中的大规模数据. Hive 定义了简单的类 SQL 查询语言,称为 HQL.它同意熟悉 ...
- C语言的##
比如说我定义一个宏:#define DECLARE_DYNAMIC(class_name) \public:static CRuntimeClass class##class_name; \virtu ...
- Fakeapp2.2安装,使用简记--------------转载自iJessie
原文:https://www.cnblogs.com/iJessie/p/8568377.html 1,硬件和操作系统,支持cuda的Nvidia显卡,8G及以上的内存,Windows10 x64(推 ...
- 深入浅出Stream和parallelStream
https://blog.csdn.net/darrensty/article/details/79283146
- Android中处理崩溃异常和记录日志(转)
现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了崩溃现象,开 ...
- Fragment 懒加载
我们在遇到Activity嵌套Fragment的时候经常会遇到多个Fragment页面同时加载数据,一开始的时候就初始化很多页面,有的甚至进入页面的时候,会造成缓慢的现象,下面我们就针对这个问题做一下 ...
- spring事务和mysql的隔离级别
mysql事务.mysql隔离级别.mysql锁.mysql数据一致性.Spring事务.Spring事务传播性之间的关系 一直有些模糊,整理一下. mysql事务: 在mysql中,只有使用了I ...
- poj 2151Check the difficulty of problems<概率DP>
链接:http://poj.org/problem?id=2151 题意:一场比赛有 T 支队伍,共 M 道题, 给出每支队伍能解出各题的概率~ 求 :冠军至少做出 N 题且每队至少做出一题的概率~ ...
- sql查询的日期判断问题
在SQLSERVE中,如果某个数据表的类型被定义成datetime类型,那么数据是包含秒的.这时候如何查询某天的数据呢?新手们可能想:最直接的做法是把时间部分去掉,只取日期部分.于是日期的函数就用上了 ...