题目

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.

原题链接(点我)

解题思路

给两个字符串S和T, 求在字符串S中删除某些字符后得到T。问一共能有多少种删除方法?

这个题用常规方法超时。

得用动态规划,动态规划最基本的就是要找到动态规划方程。

首先记:dp[i][j] 为 从S[0..j-1]中删除某些字符后得 T[0...i-1]的不同删除方法数量。

动态规划方程为: dp[i][j] = dp[i][j-1] + (S[j-1]==T[i-1] ? dp[i-1][j-1] : 0);

代码实现

class Solution {
public:
int numDistinct(string S, string T) {
int m = S.size();
int n = T.size();
if(m<n) return 0;
vector<vector<int> > dp(n+1, vector<int>(m+1, 0));
for(int i=0; i<m; ++i)
dp[0][i] = 1;
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
dp[i][j] = dp[i][j-1] + (S[j-1]==T[i-1] ? dp[i-1][j-1] : 0);
}
}
return dp[n][m];
}
};

对于辅助数组我们其有用到的也仅仅有当前行和其前一行。所以我们能够仅仅申请两行的辅助数组。优化代码例如以下:

class Solution {
public:
int numDistinct(string S, string T) {
int m = S.size();
int n = T.size();
if(m<n) return 0;
vector<int> dp1(m+1, 1);
vector<int> dp2(m+1, 0);
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
dp2[j] = dp2[j-1] + (S[j-1]==T[i-1] ? dp1[j-1] : 0);
}
dp1.clear();
dp1 = dp2;
dp2[0] = 0;
}
return dp1[m];
}
};
假设你认为本篇对你有收获,请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30043797
)

[LeetCode] Distinct Subsequences [29]的更多相关文章

  1. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  4. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  7. LeetCode: Distinct Subsequences 解题报告

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

  8. [Leetcode] distinct subsequences 不同子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

随机推荐

  1. JavaScript 网页脚本语言 由浅入深 (随笔)

    1)基础 学习目的: 1. 客户端表单验证 2. 页面动态效果 3. jQuery的基础 什么是JavaScript? 一种描述性语言,也是一种基于对象和事件驱动的,并具有安全性能的脚本语言 java ...

  2. Sting.format字符串格式化

    控制格式scanf printf 也不知道为什么=-= 越研究深层的java就越感觉它是从别的语言那抄袭来的

  3. scrapy爬虫,爬取图片

    一.scrapy的安装: 本文基于Anacoda3, Anacoda2和3如何同时安装? 将Anacoda3安装在C:\ProgramData\Anaconda2\envs文件夹中即可. 如何用con ...

  4. blog搬家啦

    本blog大概不会更新了 新blog地址:https://zykykyk.github.io/

  5. Linux下gcc与g++用法以及编写makefile

    1.         gcc与g++编译流程: 1)         编译流程: 2)         预处理:生成.i的预处理文件. Ø 只激活预处理,这个不生成文件,需要把它重定向一个输出文件. ...

  6. word-ladder总结

    title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...

  7. FindWindow和FindWindowEx

    函数原型:FindWindow(lpszClassName,lpszWindowName) 参数:lpszClassName--窗口类名;lpszWindowName--窗口标题 功能:查找窗口,未找 ...

  8. HDU 5154 Harry and Magical Computer bfs

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. RPM 打包技术与典型 SPEC 文件分析

    一 .rpm 介绍 1. 概述 RPM全称是 Red Hat Package Manager(Red Hat包管理器).几乎所有的 Linux 发行版本都使用这种形式的软件包管理安装.更新和卸载软件. ...

  10. HTTP的KeepAlive是开启还是关闭?

    HTTP的KeepAlive是开启还是关闭? http://itindex.net/detail/50719-http-keepalive 1.KeepAlive的概念与优势 HTTP的KeepAli ...