题目:

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).

Example

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的更多相关文章

  1. 【LeetCode】114. Distinct Subsequences

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  2. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  3. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. 【Leetcode】115. Distinct Subsequences

    Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...

  6. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  8. 【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, ...

  9. 【easy】118.119.杨辉三角

    这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...

随机推荐

  1. iOS 从UITableViewController中分离数据源

    之前看objc.io #1 Light View Controllers看到一个非常不错的技巧:从UITableViewController中分离数据源,这样能够减小UITableViewContro ...

  2. android 怎样加速./mk snod打包

    mm命令高速编译一个模块之后,一般用adb push到手机看效果,假设环境不同意用adb push或模块不常常改.希望直接放到image里,则能够用./mk snod,这个命令只将system文件夹打 ...

  3. Java系统中如何拆分同步和异步

    很多开发人员说,将应用程序切换到异步处理很复杂.因为他们有一个天然需要同步通信的Web应用程序.在这篇文章中,我想介绍一种方法来达到异步通信的目的:使用一些众所周知的库和工具来设计他们的系统. 下面的 ...

  4. linux uart驱动——相关数据结构以及API(二)

    一.核心数据结构      串口驱动有3个核心数据结构,它们都定义在<#include linux/serial_core.h>1.uart_driver     uart_driver包 ...

  5. nodejs实如今线群聊

    这不是一个项目而是一个适合刚開始学习的人学习的样例.主要实现了下面基本功能: 1:群聊.每个人都能够收到其它人的消息,以及能够发消息给其它人,每个人用ip地址标识. 2:显示当前在线用户. 3:每个用 ...

  6. ASP.NET MVC 4 技术讲解

    ASP.NET MVC 相关的社群与讨论区 Routing 与 ASP.NET MVC 生命周期 Model相关技术 Controller相关技术 View数据呈现相关技术 Area区域相关技术 AS ...

  7. 转载 -- 基于原生JS与OC方法互相调用并传值(附HTML代码)

    最近项目里面有有个商品活动界面,要与web端传值,将用户在网页点击的商品id 传给客户端,也就是js交互,其实再说明白一点就是方法的互相调用而已. 本文叙述下如何进行原生的JavaScript交互 本 ...

  8. 开发环境部署git 步骤

    1.安装完后,打开git bash.  vim .gitconfig 此文件放在用户目录,即 git bash 打开后默认位置. 2.插入以下内容,保存 [user] name = email = [ ...

  9. 兼容性强、简单、成熟、稳定的RTMPClient客户端拉流功能组件EasyRTMPClient

    EasyRTMPClient EasyRTMPClient拉流功能组件是EasyDarwin流媒体团队开发.提供和维护的一套非常稳定.易用.支持重连的RTMPClient工具,SDK形式提供,全平台支 ...

  10. 九度OJ 1063:整数和 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3456 解决:2254 题目描述: 编写程序,读入一个整数N. 若N为非负数,则计算N到2N之间的整数和: 若N为一个负数,则求2N到N之间 ...