题目

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. 图形文件元数据管理工具exiv2

    图形文件元数据管理工具exiv2   图形文件通常都包含多种元数据,如Exif.IPTC.XMP.这些信息往往是渗透人员收集的目标.为了便于管理这些信息,Kali Linux内置了专用工具exiv2. ...

  2. 【BZOJ 3661】 Hungry Rabbit (贪心、优先队列)

    3661: Hungry Rabbit Time Limit: 100 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 67  Solved: 4 ...

  3. QT学习笔记3:QT中语法说明

    一.Qt 类中Q_OBJECT的作用 QObject 是所有Qt对象的基类. QObject 是Qt模块的核心.它的最主要特征是关于对象间无缝通信的机制:信号与槽.使用connect()建立信号到槽的 ...

  4. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

  5. UVALive 4426 Blast the Enemy! 计算几何求重心

    D - Blast the Enemy! Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  6. hdu 刷题记录

    1007 最近点对问题,采用分治法策略搞定 #include<iostream> #include<cmath> #include<algorithm> using ...

  7. .vs目录有什么用?

    写这篇博文的目的就是方便后来者能够在百度里轻松搜到. 反正我找了半天没找到关于.vs目录的介绍,最后还是在同事的帮助下才找到的. 参考地址:https://developercommunity.vis ...

  8. cloudstack openstack zstack

    http://www.cnblogs.com/skyme/archive/2013/06/06/3118852.html http://www.niubua.com/ http://zstack.or ...

  9. xarmain使用Forms编译android工程出现support_r19.0.1.zip支持包错误

    第一次使用xarain下载Forms程序,提示一下错误. C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.t ...

  10. 报错:405 Method Not Allowed

    出现错误的原因是:静态文件不能通过post方式访问. 解决办法:改成用get方式访问.