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. 创建一个自定义颜色IRgbColor

    后续文章需要用到,很简单的一个小函数 /// <summary> /// 自定义颜色 /// </summary> /// <param name="r&quo ...

  2. python程序一直在后台运行的解决办法

    刚写了个python程序,要一直在后台运行,即使断开ssh进程也在,下面是解决办法: 假如Python程序为test.py 编写shell脚本start.sh #!/bin/bash python t ...

  3. ImageView的常用属性

    ImageView的一些常用属性,并且这些属性都有与之对应的getter.setter方法: android:adjustViewBounds:设置ImageView是否调整自己的边界来保持所显示图片 ...

  4. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ItemSelector)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...

  5. VTK初学一,比较常见的错误2

    我的开发环境: 系统:win8.1 QT:5.4.2MinGW版 VTK:6.3 按照教程生成一个球体显示在,Qt的QVTKWidget控件中,出现如下ERROR: ERROR: In D:\VTK6 ...

  6. [Hadoop] 在Ubuntu系统上一步步搭建Hadoop(单机模式)

    1 Hadoop的三种创建模式 单机模式操作是Hadoop的默认操作模式,当首次解压Hadoop的源码包时,Hadoop无法了解硬件安装环境,会保守地选择最小配置,即单机模式.该模式主要用于开发调试M ...

  7. ASP.NET AJAX调用 WebService

    同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...

  8. Scrum Meeting ——总结

    冲刺总结 0*.燃尽图 迟来的燃尽图,别看它是最后一天掉了一堆,感觉很假,像是人为的把issues都关闭掉.其实不然,很多功能是大家平时做好,但是没整合在一起,所以没燃掉,在最后几天的整合中,通过测试 ...

  9. CentOS 7下安装Mono

    最近的项目中需要用到Linux作为服务器,而我们的开发技术是基于.NET的,所以只能在CentOS 7上尝试着安装一下Mono,下面是具体的安装步骤: 1.安装一些必备的依赖项 yum -y inst ...

  10. mysql取前取后

    SELECT * FROM (SELECT * FROM 表 WHERE id<居中的ID ORDER BY id DESC LIMIT 5) as A UNION all SELECT * F ...