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. Array-快餐管饱

    一.如何获得一个数组? rsp: 1. []  2.new Array() 3.str.split() ps:new Array()可以不加括号,其传一个参数代表数组长度,两个及以上就是初始化数组. ...

  2. 利用html2canvas将当前网页保存为图片.

    先分析下这个技术可实现的方式,以及优缺点吧! 前端实现 缺点是:兼容性查,需要高级浏览器支持,因为需要支持 canvas 绘图,还有就是会操作 html5 canvas api.(如果不会使用canv ...

  3. scrapy框架爬取笔趣阁完整版

    继续上一篇,这一次的爬取了小说内容 pipelines.py import csv class ScrapytestPipeline(object): # 爬虫文件中提取数据的方法每yield一次it ...

  4. count_char

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

  5. vue---day04

    1. Node.js 1.1 介绍: - Node.js 是一个JavaScript运行环境,实质上是对Chrome V8引擎的封装. - Node.js 不是一个 JavaScript 框架,不同于 ...

  6. Redis 在springBoot中的一个使用示例

    在现系统中使用了一个字典表,更新或插入字典表需要做Redis缓存 @Override @Cache(name = Constants.REDIS_PREFIX_DIC, desc = "变更 ...

  7. 笔记-python-动态添加属性

    笔记-python-动态添加属性 1.      添加对象/类属性 添加对象属性 class Person(object): def __init__(self, newName, newAge): ...

  8. JAVA大作业汇总1

    JAVA大作业 代码 ``` package thegreatwork; import javafx.application.; import javafx.scene.control.; impor ...

  9. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  10. (1)分布式下的爬虫Scrapy应该如何做-安装

    关于Scrapy的安装,网上一搜一大把,一个一个的安装说实话是有点麻烦,那有没有一键安装的?答案显然是有的,下面就是给神器的介绍: 主页:http://conda.pydata.org/docs/ 下 ...