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. linux 安装 ArcSDE10.1

    实验仍未成功,步骤仅供参考. 1:首先检查一下在Linux操作系统下Oracle数据库是否能启动,是否能连通等 [oracle@localhost ~]$ sqlplus SQL*Plus: Rele ...

  2. Git 学习笔记参考

    1.参考学习资料 网上资料: http://www.cnblogs.com/aoguren/p/4189086.html http://www.liaoxuefeng.com/wiki/0013739 ...

  3. CSS知识回顾--读《CSS 那些事儿》笔记

    由于之前有了解过CSS的相关知识,有了一定的基础,所以读起<CSS 那些事儿>不是很有难度,况且我现在读起来时,CSS3和HTML5比较流行,这里只是记录一些CSS知识记录,不做详细铺开, ...

  4. 【09-04】java内部类学习笔记

    java中的内部类 静态内部类 成员内部类 方法内部类 匿名内部类 1.静态内部类 class Outer { private static String outer = "outer&qu ...

  5. Node.js Stream - 实战篇

    邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...

  6. Silicon C8051F340之时钟系统

    一.背景 做个记录,以备下次快速开发. 二.正文 C8051F340有一个可编程内部高频振荡器.一个可编程内部低频振荡器.一个外部振荡器驱动电路 和一个4倍时钟乘法器.其中可编程内部高频振荡器在系统复 ...

  7. c++ 类静态成员、非静态成员初始化

    1.静态成员初始化(不能在构造函数或初始化列表中初始化) 1.1 所有静态成员都可以在类定义之外初始化(通用),如下所示 class test { public: static int a; }; ; ...

  8. C#实现K-MEDOIDS聚类算法

    1.任意选取K个对象作为初始聚类中心(O1,O2,…Oi…Ok). 2)将余下的对象分到各个类中去(该对象与哪一个聚类中心最近就被分配到哪一个聚类簇中): 3)对于每个类(Oi)中,顺序选取一个Or, ...

  9. c/c++ string.h

    操作数的都是 ( char * )型,操作时不考虑末尾的'\0',操作数必须是指向字符串的指针("a"),不能是字符('a'). size_t strlen( const char ...

  10. ASM:《X86汇编语言-从实模式到保护模式》第9章:实模式下中断机制和实时时钟

    中断是处理器一个非常重要的工作机制.第9章是讲中断在实模式下如何工作,第17章是讲中断在保护模式下如何工作. ★PART1:外部硬件中断 外部硬件中断是通过两个信号线引入处理器内部的,这两条线分别叫N ...