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. js -去掉首尾的空格.

    function trimFE (str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }

  2. ios后台程序持续运行方法

    iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到系统的很多限制,这样可以提高电池的使用和用户体验.但是有很多社交类的软件,如果因为它在后台就不能刷新到新的数据的话 ...

  3. 怎样在win7上远程连接linux系统

    window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤   在前面的环境和软件都有的情况下,双击putty.exe软件,如下图:   在软件界面中的:Hos ...

  4. WPF 启动初始界面

    不经意间发现了wpf的这个小玩意,感觉蛮有意思的.我在项目中添加了一张图片 如图: wpf-1.JPG(10.73 K) 2010-6-6 17:04:47 然后再这张图片的属性中设置它的生成操作为S ...

  5. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  6. phpstorm集成phpunit(转)

    转自http://blog.csdn.net/zhuziying99/article/details/49028321 phpstorm集成phpunit1.下载phpunit.phar,将该文件放到 ...

  7. 桂电在线-转变成bootstrap版

    由于angularjs的不熟悉,而且SEO需要学习更多东西,于是先采用bootstrap版本,毕竟工作上也需要使用bootstrap,然后参照视频教程学习. bootstrap 基本模板 <!D ...

  8. 帝国cms 列表页分页样式修改美化【2】

    上一篇(帝国cms 列表页分页样式修改美化[1])中我们已经对分页说了一个大概,下面我们就自己动手弄一个分页把: 第一步:进入帝国cms后台,点击系统设置->系统参数设置->信息设置:里面 ...

  9. 用Django搭建个人博客—(1)

    业精于勤荒于嬉,形成于思毁于随. 本阶段的任务小记: 简单介绍一下Django的使用,创建项目和一个app 简单介绍一下Django的settings.py文件的相关配置 整合数据库到自己的博客系统中 ...

  10. python调用java

    这么个标题多少有点蛋疼的感觉,两个都是互联网时代的语言,学习成本和执行效率也差不多,之所以会产生这种需求,多半是想在python中引用java的类,例如安卓和hadoop的生态圈,基本是java代码的 ...