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, 求 T 在 S 中有多少个不同的子序列。

一开始看到这道题,没有思路,在网上看了别人的解答才了解到如何解题。这是一道 dp 题,如果能想得到状态转义 关系,答案就比较明显了。解法有点想 背包问题的解法。

二维数组 vv[T.len][S.len] 存储中间结果。 vv[i][k], 表示 T[0, i] 在 S[0, k] 中的不同的子序列个数。

状态转换关系,文字描述:

  当 S[i] 和 T[k] 不相等时, T[0, i] 在 S[0, k] 的不同子序列个数,和在S[0, k-1] 的不同子序列个数相同。

  当 S[i] 和 T[k] 相同时, T[0, i] 在 S[0, k] 的不同子序列个数等于 S[i] 被采取的情况( vv[i-1][k-1] ), 加上 S[i] 不被采取的情况 ( vv[i][k-1] )。

状态转换关系,公式表示:

  当 S[i] != T[k] 时,vv[i][k] = vv[i][k-1]

  当 S[i] == T[k] 时,vv[i][k] = vv[i-1][k-1] + vv[i][k-1]

 int numDistinct(string s, string t) {

     if (t.size() > s.size()) {
return ;
} vector<vector<int>> vv(t.size(), vector<int>(s.size(), -)); // 边界值处理
for (int i = ; i < vv.size(); i++) {
vv[i][i-] = ;
} // 边界值处理
if (s[] == t[]) {
vv[][] = ;
}else{
vv[][] = ;
} // 边界值处理
for (int i = ; i < vv[].size(); i++) {
if (t[] == s[i]) {
vv[][i] = vv[][i-] + ;
}else{
vv[][i] = vv[][i-];
}
} // 状态转移
for (int i = ; i < vv.size(); i++) {
for (int k = i ; k < vv[i].size(); k++) {
if (t[i] != s[k]) {
vv[i][k] = vv[i][k-];
}else{
vv[i][k] = vv[i-][k-] + vv[i][k-];
}
}
} return vv[t.size()-][s.size()-];
}

[LeetCode] Distinct Subsequences 解题思路的更多相关文章

  1. LeetCode: Distinct Subsequences 解题报告

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

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

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

  3. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

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

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

  5. [leetcode]Distinct Subsequences @ Python

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

  6. [LeetCode] Distinct Subsequences [29]

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

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

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

  8. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. LeetCode: Distinct Subsequences [115]

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

随机推荐

  1. javascript 动态操作Html

    <html> <body> <p>aaaaa</p> <input type="button" value="con ...

  2. Windows下的多线程

    Windows下的进程和Linux下的进程是不一样的,它比较懒惰,从来不执行任何东西,它只是为线程提供执行环境,然后由线程负责执行包含在进程的地址空间中的代码.当创建一个进程的时候,操作系统会自动创建 ...

  3. web应用程序servlet的映射名称的规则及请求过程

    首先用MyEclipse创建一个web Project(工程名起为TestServletProject),新建一个Servlet(这里servlet的名字起TestServlet),将请求的servl ...

  4. ACM YTU 2018 母牛的故事

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. 网站访问架构cdn与负载均衡

    曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需 要从无数个角度去考虑他,大到服务器的布局,小到软件中某个 ...

  6. 两个winform窗体同步

    /// <summary>        /// 初始left距离        /// </summary>        int initx = 0;        /// ...

  7. C#程序:如何创建xml文件以及xml文件的增、删、改、查

    其实今天的这篇博文 ,是对请几天发表的博文的一个总结,只是想把xml文件的增删改查结合起来,这样更容易让初学的朋友理解,废话也不多说了,开始吧! 下面是我把我在vs环境下写的代码ctrl+V然后ctr ...

  8. JavaScript学习总结【9】、DOM Ready

    1.DOM DOM(Document Object Model)即文档对象模型,是从文档中抽象出来的,DOM 操作的对象就是文档,DOM 将 HTML 文档呈现为带有元素.属性和文本的树结构,即节点树 ...

  9. frameset标签代码实现网站跳转

    js代码1: document.writeln("<frameset rows=\"0, *\">"); document.writeln(&quo ...

  10. PHP的类自动加载机制

    在PHP开发过程中,如果希望从外部引入一个class,通常会使用include和require方法,去把定义这个class的文件包含进来. 这个在小规模开发的时候,没什么大问题.但在大型的开发项目中, ...