题目:

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.

代码:

class Solution {
public:
int numDistinct(string s, string t) {
const int len_s = s.size();
const int len_t = t.size();
if ( len_s<len_t ) return ;
if ( len_s==len_t ) return s==t;
vector<vector<int> > dp(len_t+,vector<int>(len_s+,));
// initialization
dp[][] = ;
for ( int i=; i<=len_s; ++i ) dp[][i] = ;
// dp process
for ( int i=; i<=len_t; ++i )
{
for ( int j=; j<=len_s; ++j )
{
if ( t[i-]!=s[j-] )
{
dp[i][j] = dp[i][j-];
}
else
{
dp[i][j] = dp[i][j-] + dp[i-][j-];
}
}
}
return dp[len_t][len_s];
}
};

tips:

这个题目第一次想到的办法是递归:统计一共要删除多少个字符;每层递归删除一个字符;扫描所有情况。 这种递归的解法大集合肯定会超时。

憋了一会儿dp的解法,这次又把问题想简单了。

这种字符串比较的题目,涉及到记忆化搜索的,可以用二维dp来做。

dp[i][j] 表示T[0:i-1]与S[0:j-1]的匹配次数。

1. 初始化过程,dp[0][i]代表T为空字符,则S若要匹配上T只有把所有字符都删除了一种情况。

2. 状态转移方程:dp[i][j]如何求解?

 这里的讨论点其实很直观,即S[j-1]这个元素到底是不是必须删除。

  a) 如果T[i-1]!=S[j-1],则S[j-1]必须得删除(因为这是最后一个元素,不删肯定对不上T[i-1]); 因此 dp[i][j] = dp[i][j-1],跟S少用一个字符匹配的情况是一样的。

  b) 如果T[i-1]==S[j-1],则S[j-1]可删可不删;因此dp[i][j] = dp[i][j-1] + dp[i-1][j-1]:

    “dp[i][j-1]”是把S[j-1]删了的情况;“dp[i-1][j-1]”是不删除S[j-1]的情况,即用S[j-1]匹配上T[i-1]

按照上述的分析过程,可以写出来DP过程。完毕。

===========================================

第二次过这道题,没想出来思路,照着之前的思路写的。

class Solution {
public:
int numDistinct(string s, string t) {
int dp[s.size()+][t.size()+];
fill_n(&dp[][], (s.size()+)*(t.size()+), );
dp[][] = ;
for ( int i=; i<=s.size(); ++i ) dp[i][] = ;
// dp process
for ( int i=; i<=s.size(); ++i )
{
for ( int j=; j<=t.size(); ++j )
{
if ( s[i-]==t[j-] )
{
dp[i][j] = dp[i-][j-] + dp[i-][j];
}
else
{
dp[i][j] = dp[i-][j];
}
}
}
return dp[s.size()][t.size()];
}
};

【Distinct Subsequences】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【N-Quens II】cpp

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  4. 【Climbing Stairs】cpp

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  5. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  7. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  8. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  9. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

随机推荐

  1. Tomcat8

    一.Apache Tomcat 8介绍 Tomcat 8.0.0-RC3 (alpha) Released                                                ...

  2. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:9.观察者模式

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 在一个程序的迭代过程中,复杂度渐渐上升,可能会出现一些跨模块的调用的需求,若是直接得到引用来进行使用,会导致模 ...

  3. Hyper-V 2016 配置管理系列(部署篇)

    Hyper主机前提准备以后,我们开始Hyper-V Cluster 群集配置 准备验证Cluster 群集 : 1)打开群集管理器,点击"validate Configuration&quo ...

  4. RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍

    1. AMF AMF(是Action Message Format的缩写)是在flash和flex中与远程服务端交换数据的一种格式.它是二进制格式,Flash应用与服务端或数据库通过RPC交换数据时, ...

  5. windows下php7.1.5、mysql环境搭建

    php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...

  6. IOS 某个控件出不来原因(经验分享)

    某个控件出不来:(检查原因) 1.frame的尺寸和位置对不对 2.hidden是否为YES 3.有没有添加到父控件中 4.alpha 是否 < 0.01 5.被其他控件挡住了 6.父控件的前面 ...

  7. 【洛谷2152】[SDOI2009] SuperGCD(Python好题)

    点此看题面 大致题意: 给你两个长度\(\le10000\)的正整数,让你求它们的\(gcd\). Python​ 高精请绕道. 这题的正解应该是Python. 对于这种高精题,肯定是Python最方 ...

  8. Mybatis- 配置

    主配置文件 properties 第一种 <properties> <property name="jdbc.driver" value="com.my ...

  9. final关键字,static关键字

    Final final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 注意: 被final修饰的类不能被继承但可以继承别的类 class Yy ...

  10. 短短几行css代码实现滚动条效果

    如何实现使用css实现滚动条效果 实现效果,运用线性渐变来实现功能 假设我们的页面被包裹在 <body> 中,可以滚动的是整个 body,给它添加这样一个从左下到到右上角的线性渐变: bo ...