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相等的个数,如果熟悉《算法导论》的话,可以想到动态规划那一节关于由一个字符串通过增删变成另一个字符串解法

本题利用动态规划求解

设决策变量为path[i][j],表示S[1..i]的前i个字符通过删除字符变成T[1..j]的个数

那么状态转移方程为

                       path[i-1][j-1]  (S[i] == T[j])

path[i][j] = path[i-1][j](删除S的第i个字符)+                  (不删除字符)

                          0       (S[i] != T[j] )

class Solution {
public:
int numDistinct(string S, string T) {
int n = S.length(), m=T.length();
if(n <= m) return S==T;
vector<vector<int> > path(n+,vector<int>(m+,));
for(int i = ; i <=n ; ++ i) path[i][] = ;
for(int j =; j <= m; ++ j){
for(int i = ; i <= n ; ++ i){
path[i][j] = path[i-][j] + (S[i-]==T[j-] ? path[i-][j-] : );
}
}
return path[n][m];
}
};

Leetcode Distinct Subsequences的更多相关文章

  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 [115]

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

  6. LeetCode: Distinct Subsequences 解题报告

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

  7. [LeetCode] Distinct Subsequences [29]

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

  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. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

  2. eclipse中 linked resource的使用

    一.关于linked resource   eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...

  3. [nosql之缓存memcache]安装篇LInux for Windows

    首先呢在PHP开发的过程中会用到很多缓存服务,从而提升访问质量或者临时存储一些数据. 优点 结构简单,读取速度快,易于维护.还有一些特性memcache redis mongodb都可以用来做为缓存用 ...

  4. 精选9个值得学习的 HTML5 效果【附源码】

    这里精选了一组很酷的 HTML5 效果.HTML5 是现 Web 开发领域的热点, 拥有很多让人期待已久的新特性,特别是在移动端,Web 开发人员可以借助 HTML5 强大功能轻松制作各种交互性强.效 ...

  5. css浏览器兼容问题

    https://www.douban.com/group/topic/4629864/

  6. Mac Pro 安装 Sublime Text 3,个性化设置,主题 和 插件 收藏

    1.到官网下载安装包 http://www.sublimetext.com/3 2.附注册码一枚 Sublime Text 3 3126 —– BEGIN LICENSE —– Alexey Plut ...

  7. jenkins自动化构建iOS应用配置过程中遇到的问题

    最近配置jenkins来自动构建iOS应用,期间遇上不少问题.在这里分享给大家,也给自己留个底,方便下次解决问题. 首先说明下基本情况,我们因为部署jenkins的机器不是Mac,所以不能安装Xcod ...

  8. angular-ui-bootstrap-modal必须要说的几个点(转)

    angular-ui-bootstrap-modal必须要说的几个点 摘要: 基于angular来实现的一个bootstrap模态框,有些不得不说的地方 项目中以前就经常用到模态框,但是一直没有时间来 ...

  9. 2016年11月24日--面向对象、C#小复习

    面对对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象.对同类对象抽象出其共性,形成类.类中的大多数数据,只能用本类的方法进行处理.类通过一个简单的外部接口与外界发生关系,对象 ...

  10. JSON.stringify()和JSON.parse()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...