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. Yaf学习(一)----Linux安装Yaf

    1.简介 Yaf,全称 Yet Another Framework,是一个高性能的PHP开发框架,采用PHP扩展实现(c语言).Blablablabla....... 2.环境 2.1 虚拟机 虚拟机 ...

  2. Hadoop(8)-HDFS的读写数据流程以及机架感知

    1. HDFS的写数据流程 1.客户端通过fs模块向NameNode申请文件上传,NameNode检查请求是否合法,如用户权限,目标文件是否已存在,父目录是否存在等等 2.NameNode返回是否可以 ...

  3. 一、Linux知识体系结构图

    参考: https://blog.csdn.net/Swing_Liu/article/details/79202479

  4. python3 练习题100例 (二十七)列表元素改写

    题目内容: 输入一个列表alist,要求列表中的每个元素都为正整数且不超过10: 将列表中的奇数变为它的平方,偶数除以2后打印新的列表(新的列表中所有元素仍都为整数). 可以使用以下实现列表alist ...

  5. Python学习手册之控制结构(一)

    在上一篇文章中,我们对 Python 进行了简单介绍和介绍了 Python 的基本语法,现在我们继续介绍 Python 控制结构. 查看上一篇文章请点击:https://www.cnblogs.com ...

  6. Unicode控制字符

    Unicode控制字符就是特殊的Unicode字符 控制字符转义代码对照表 Unicode-控制字符 ‎LRM ‏RLM ‍ZWJ ‌ZWNJ ‪LRE ‭LRO ‮RLO ‬PDF NADS  ...

  7. 纯js生成QRCode

    纯js,不依赖jquery,非常好用,废话不多说,直接上代码! <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  8. 【HNOI2008】玩具装箱TOY & 斜率优化学习笔记

    题目 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为 \(1\cdots N\ ...

  9. SVN被锁定如何解决?

    报错: “E:/SVN被锁定” 请进行清理操作 解决方式: 1.试着右击进行解锁 没有解决 2.试着进行清理,清理后再做更新操作 好的成功. OK能正常使用了. 2017年10月26日更新 报错: s ...

  10. Qt Qml 汽车仪表

    上一个原文连接http://blog.csdn.net/z609932088/article/details/53946245 参考资料连接:链接: https://pan.baidu.com/s/1 ...