【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 ...
随机推荐
- 用汇编的角度剖析c++的virtual
多态是c++的关键技术,背后的机制就是有一个虚函数表,那么这个虚函数表是如何存在的,又是如何工作的呢? 当然不用的编译器会有不同的实现机制,本文只剖析vs2015的实现. 单串继承 首先看一段简单的代 ...
- jQuery入门知识点
<精通ASP.NET MVC3框架>第20章 1.jQuery文件jquery-1.5.1.js:jquey核心库常规版jquery-1.5.1.min.js:jquery核心库最小化版j ...
- 使用Reveal来查看别人的APP界面+白苹果不刷机解决方式
Reveal这个强大的界面调试工具.能够实时查看.改动view的属性,大体上实现了iOS程序猿梦寐以求的功能.比方,有时候我们加入了一个view,可是那个view死活不显示出来,这时候祭出Reveal ...
- 如何给UIViewController瘦身
本文转载至 http://www.cocoachina.com/ios/20141128/10356.html 随着程序逻辑复杂度的提高,你是否也发现了App中一些ViewController的代码 ...
- redis启动错误-- Creating Server TCP listening socket *:6379: listen: UnKnown error
前提:windows server 2008.redis 3.x 今天给服务器部署redis环境,文件配置.服务安装都很顺利,可就在启动服务的时候提示 百度老半天也没找到个说到点子上的. 这里记录下解 ...
- linux自动ftp上传与下载文件的简单脚本
#!/bin/sh cd /data/backup/55mysql DATE=`date +'%Y%m%d'`file="55_mysql_"$DATE"03*.rar& ...
- 关于TransactionScope 使用
在去年的项目中使用了TransactionScope,现在总结下TransactionScope的使用说明 一.TransactionScope是.Net Framework 2.0之后,新增了一个名 ...
- iOS开发常用第三方框架
1.网络通信 1.ASIHTTPRequest 这是一个经典的老库,功能完全而强大,但已经停止更新很久了(iOS5.0停止更新,但是我最近看github上这个项目有新改动).在不同iOS版本上略微有一 ...
- wx.onNetworkStatusChange(function (res) 监听网络状态变化 实践方案
网络状态 · 小程序 https://developers.weixin.qq.com/miniprogram/dev/api/device.html#wxonnetworkstatuschangec ...
- 我的Java开发学习之旅------>Java经典排序算法之希尔排序
一.希尔排序(Shell Sort) 希尔排序(Shell Sort)是一种插入排序算法,因D.L.Shell于1959年提出而得名. Shell排序又称作缩小增量排序. 二.希尔排序的基本思想 希尔 ...