Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard)

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.

分析:

看题目感觉就跟LCS很像,考虑用双序列动态规划解决。

1. 状态:

dp[i][j]表示从第一个字符串前i个组成的子串转换为第二个字符串前j个组成的子串共有多少种方案。

2. 递推:

s[i - 1] == t[j - 1], 则dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];

s[i - 1] != t[j - 1],则dp[i][j] = dp[i - 1][j];

3. 初始化:

dp[i][0] = 1(删除到没有字符只有一种方案)

4. 返回值:

dp[sz1 - 1][sz2 - 1]

代码:

 class Solution {
public:
int numDistinct(string s, string t) {
int sz1 = s.size(), sz2 = t.size();
int dp[sz1 + ][sz2 + ];
memset(dp, , sizeof(dp));
for (int i = ; i < sz1; ++i) {
dp[i][] = ;
}
for (int i = ; i <= sz1; ++i) {
for (int j = ; j <= sz2; ++j) {
if (s[i - ] == t[j - ]) {
dp[i][j] = dp[i - ][j - ] + dp[i - ][j];
}
else {
dp[i][j] = dp[i - ][j];
}
}
}
return dp[sz1][sz2];
}
};

LeetCode115 Distinct Subsequences的更多相关文章

  1. [Swift]LeetCode115. 不同的子序列 | Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  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. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  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(115) Distinct Subsequences

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

  6. [Leetcode][JAVA] Distinct Subsequences

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

  7. Distinct Subsequences Leetcode

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

  8. 【leetcode】Distinct Subsequences(hard)

    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. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  2. PHP CURL header 设置HOST主机头进行访问并 POST提交數據

    $host = array("Host: act.qzone.qq.com");// 域名不帶http://$data = array(            'aa' => ...

  3. 一次.NET项目反编译的实战经验(WinForm)

    最近由于业务需求,需要对一个老项目进行功能调整.但是项目的源代码已经找不到了.所以只能尝试对项目行进反编译. 一.反编译工具的选择 提到.NET的反编译,第一个想到的就是大名鼎鼎的Reflector. ...

  4. agc014F Strange Sorting

    这套题比较简单,以为自己能够独立A掉D和E,或许就能自己A掉F,看来还真是想多了 题意:给一个$n$的全排列,每次操作把$max(a[1],a[2],...,a[i]) = a[i]$的记为$high ...

  5. Thinkphp 错误集锦

    1.无法加载控制器 开始还跑TP核心文件中找错误,结果没找到什么结果.最后还是用程序新建模块才发现问题. 问题是命名空间名字写错了.比如书:本来是Report模块下的IndexContrller,但是 ...

  6. 利用jquery模拟select效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. ORACLE时间常用函数(字段取年、月、日、季度)

    TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal tw ...

  8. C++/CLI 创建WPF程序

    本文简单演示下用C++/CLI创建WPF程序,IDE为VS2015 首先创建CLR项目,选择CLR空项目: 然后,右键源文件,选择新建class,选择CLR->Component Class 接 ...

  9. 快速乘O(1)和O(log)

    O(1)快速乘来自骆可强:<论程序底层优化的一些方法与技巧> //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MO ...

  10. 洛谷P1514 [NOIP2010提高组T4]引水入城

    P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城 ...