115. Distinct Subsequences (String; DP)
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)的更多相关文章
- [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 ...
- 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 ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
- 115. Distinct Subsequences *HARD* -- 字符串不连续匹配
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 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 ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- [转][SVN]常用操作
1. Commit 提交当前代码到 SVN 服务器. 2. 引用第三方类库时,不要从安装位置引用,而是在解决方案下,添加一个 lib 的目录,把需要的程序集复制到这里,然后从 lib 目录引用. 3 ...
- 1062 Talent and Virtue (25 分)
1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...
- CSS Web安全字体组合
常用的字体组合 font-family属性是多种字体的名称,作为一个"应变"制度,以确保浏览器/操作系统之间的最大兼容性.如果浏览器不支持的第一个字体,它尝试下一个的字体. 你想要 ...
- Oracle中查询表的大小、表的占用情况和表空间的大小
有两种含义的表大小.一种是分配给一个表的物理空间数量,而不管空间是否被使用.可以这样查询获得字节数: select segment_name, bytes from user_segments whe ...
- 解决Visual Studio “无法导入以下密钥文件”的错误
错误1无法导入以下密钥文件: Common.pfx.该密钥文件可能受密码保护.若要更正此问题,请尝试再次导入证书,或手动将证书安装到具有以下密钥容器名称的强名称 CSP: VS_KEY_ 1110Co ...
- UVA-575-水题-模拟
题意: 按照这个公式模拟 10120skew = 1×(25 −1)+0×(24 −1)+1×(23 −1)+2×(22 −1)+0×(21 −1) = 31+0+7+6+0 = 44. #inclu ...
- rsync同步web数据
rsync远程同步web服务器的数据 实验拓扑 服务器A(rsync服务器)--------------服务器B( ...
- bootstrap3中模态框的数据编辑使用方法
模态框是bootstrap3中比较好用得弹窗控件,这回使用了 说主要的,官方详细教程 http://www.runoob.com/bootstrap/bootstrap-modal-plugin.ht ...
- springboot整合ribbitMQ
参考:https://blog.csdn.net/a13627210064/article/details/82348059 参考:https://blog.csdn.net/u010288264/a ...
- jpa 多对多
entity Item package entity; import java.util.HashSet; import java.util.Set; import javax.persisten ...