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.

思路:

dp[i][j]表示 # of T[0...j-1] in S[0...i-1] (dp[0][0]表示s=NULL,t=NULL的情况)

如果S[i]!=T[j],那么dp[i][j]=dp[i-1][j]

如果S[i]=T[j],dp[i][j]=dp[i-1][j]+j抽出的情况=dp[i-1][j]+dp[i-1][j-1] (注意:这里并不是简单的dp[i-1][j]+1, j抽出后,dp[i-1][j-1]是要大于dp[i-1][j]的)

class Solution {
public:
int numDistinct(string s, string t) {
int sLen = s.length();
int tLen = t.length();
vector<vector<int>> dp(sLen+, vector<int>(tLen+,));
for(int i = ; i <= sLen; i++){ //if t==NULL, 1 method to match
dp[i][]=;
} for(int i = ; i <=sLen; i++){
for(int j = ; j <= tLen; j++){
if(s[i-]==t[j-]){
dp[i][j]=dp[i-][j]+dp[i-][j-];
}
else{
dp[i][j]=dp[i-][j];
}
}
}
return dp[sLen][tLen];
}
};

115. Distinct Subsequences (String; DP)的更多相关文章

  1. [LeetCode] 115. Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  2. leetcode 115 Distinct Subsequences ----- java

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

  3. 115. Distinct Subsequences

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

  4. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  5. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  7. 115. Distinct Subsequences *HARD* -- 字符串不连续匹配

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

  8. Java for LeetCode 115 Distinct Subsequences【HARD】

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

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 【转载】CSS + DIV 实现局部布局

    HTML CSS + DIV实现局部布局 1.本章教大家掌握2种布局方式: 1)顶部导航菜单布局,效果图: 2)购物版块布局,效果图: 2.技术目标:使用div + ul-li实现导航菜单布局    ...

  2. 廖雪峰Java1-2程序基础-8字符和字符串

    1.字符类型char char是基本的数据类型 char:保存一个字符,如英文字符.中文字符. Java使用unicode表示字符,可以将char赋值给int类型,查看字符对应的unicode编码. ...

  3. Jquery判断IE6等浏览器的代码

    这好像是由几篇代码接在一起的,原文均来自网络,不记得出处了~ jquery中利用navigator.userAgent.indexOf来判断浏览器类型,并进行了一下处理,如果不想使用jquery,稍为 ...

  4. [UE4]移动设备贴图消失

    pc版本是支持直接使用psd文件作为贴图文件,但移动设备就不支持了.

  5. (转)RRU交织冗余在LTE-R组网中的应用研究

    RRU交织冗余在LTE-R组网中的应用研究 王 芳1,2 庞萌萌1,2 (1.北京全路通信信号研究设计院集团有限公司,北京 100070; 2.北京市高速铁路运行控制系统工程技术研究中心,北京 100 ...

  6. 基于WMI获取本机真实网卡物理地址和IP地址

    using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...

  7. RESTful API & Swagger入门

    什么是RESTful API? 原文地址:https://blog.csdn.net/hjc1984117/article/details/77334616 Swagger入门教程 https://w ...

  8. load data

    LOAD DATA INFILE 'D:\GX\\mm.txt' REPLACE INTO TABLE mm FIELDS TERMINATED BY ' ' // 以空格为分隔符插入数据,前提是mm ...

  9. Using Celery with Django

    参考1: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-celery-with-d ...

  10. SQL2014还原到2008

    请使用with move选项来标识该文件的有效位置 sqlserver用命令还原数据库 restore   database   TT     from   disk='E:\test.bak'    ...