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"

Return3.

题意:给定两个字符串,字符串S中能有几个T的字符串。字符串是不改变字符的相对位置,只是删减S中一部分字符,形成T,返回删减方式的种类。

思路:动态规划。维护一个二维数组dp[T.size()+1][S.size()+1],其中dp[i][j],表示T中[0,i-1]区间的子字符串和S中[0,j-1]匹配的个数.这里要注意的是,二维数组的下标和T、S中下标的对应关系,二维数组的比字符串中在左侧和上侧多一行和一列,多得为T和S中有一个为空字符串时的情况。以S ="rabbbit", T ="rabbit"得出相应的dp数组,如下:

得出状态转移方程为:dp[i][j]=dp[i][j-1]+(T[i-1]==S[j-1]?dp[i-1][j-1]:0);  (注意字符串和数组之间的下标转换),所以整体的思路是,先赋值第一行和第一列,然后由状态方程得出其他值,代码如下:

 class Solution {
public:
int numDistinct(string S, string T)
{
int dp[S.size()+][T.size()+];
for(int i=;i<T.size()+;++i)
dp[][i]=;
for(int i=;i<S.size()+;++i)
dp[i][]=; for(int i=;i<T.size()+;i++)
{
for(int j=;j<S.size()+;j++)
{
dp[i][j]=dp[i][j-]+(T[i-]==S[j-]?dp[i-][j-]:);
}
}
return dp[T.size()][S.size()];
}
};

网友Code Gander只使用一个一维数组便实现了这个过程。代码如下:

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

[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 解题思路

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

  4. LeetCode: Distinct Subsequences [115]

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

  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 解题报告

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

  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] Distinct Subsequences [29]

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

  9. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

随机推荐

  1. 关于<meta name="viewport" content="width= device-width,user-scalable= 0,initial-scale= 1.0,minimum-scale= 1.0">

    <meta name="viewport" content=" width= device-width, user-scalable= 0, initial-sca ...

  2. js实现前端的搜索历史记录

    最近在对接前台页面(WEB端)时,产品要求需记录下客户的搜索记录,我们是前后台完全分离的项目,根本不能保存的session域中,没办法,虽然作为后台开发,遇到需求就自己研究了一通,先看一下最终效果图, ...

  3. ubuntu 18 lnmp

    1安装Nginx sudo apt-get install nginx 2安装PHP sudo apt- php7.-fpm 3安装mysql sudo apt-get install mysql 启 ...

  4. Python学习:运算符

    简单运算符: +(加) 两个对象相加 -(减) 从一个数中减去另一个数,如果第一个操作数不存在,则假定为零 *(乘) 给出两个数的乘积,或返回字符串重复指定次数后的结果   Eg.'haha' * 3 ...

  5. 03 mysql补充 (进阶)

    增加字段 alter table tb1 add age int first; 增加到第一 alter table tb1 add sex int after id; # 改变位置,id是字段名字 a ...

  6. 通过SVI实现VLAN间通信

    两个不同网段的计算机与三层交换机直连,通过SVI实现VLAN间通信vlan 1 //几个不同网段就创建几个VLANvlan 2 int f0/1 //划分VLANswitchport mode acc ...

  7. count_char

    import java.util.Scanner; public class count_char { public static void main(String args[]) { int cou ...

  8. Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph

    题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...

  9. 从0开始 java 网站开发(jsp)【1】

    前提:安装java 并配置环境变量 java下载地址: http://www.java.com/zh_CN/ 环境变量配置 本地PC路径: 电脑--属性--高级--环境变量 在系统变量中: 新建 名: ...

  10. python2.7入门---file(文件)&OS 文件&目录方法

        首先file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.f ...