LeetCode115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard)
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
.
分析:
看题目感觉就跟LCS很像,考虑用双序列动态规划解决。
1. 状态:
dp[i][j]表示从第一个字符串前i个组成的子串转换为第二个字符串前j个组成的子串共有多少种方案。
2. 递推:
s[i - 1] == t[j - 1], 则dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
s[i - 1] != t[j - 1],则dp[i][j] = dp[i - 1][j];
3. 初始化:
dp[i][0] = 1(删除到没有字符只有一种方案)
4. 返回值:
dp[sz1 - 1][sz2 - 1]
代码:
class Solution {
public:
int numDistinct(string s, string t) {
int sz1 = s.size(), sz2 = t.size();
int dp[sz1 + ][sz2 + ];
memset(dp, , sizeof(dp));
for (int i = ; i < sz1; ++i) {
dp[i][] = ;
}
for (int i = ; i <= sz1; ++i) {
for (int j = ; j <= sz2; ++j) {
if (s[i - ] == t[j - ]) {
dp[i][j] = dp[i - ][j - ] + dp[i - ][j];
}
else {
dp[i][j] = dp[i - ][j];
}
}
}
return dp[sz1][sz2];
}
};
LeetCode115 Distinct Subsequences的更多相关文章
- [Swift]LeetCode115. 不同的子序列 | Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- Leetcode563.Binary Tree Tilt二叉树的坡度
给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. 示例: 输入: 1 / ...
- Java集合(七)--基于jdk1.8的HashMap源码
HashMap在开发中经常用,面试源码方面也会经常问到,在之前也多次了解过源码,今天算是复习一下,顺便好好总结一下,包括在后面有 相关面试题.本文不会对红黑树代码由太多深入研究,特别是删除方面太复杂, ...
- java 7,8 排序异常
排序报 java.lang.IllegalArgumentException: Comparison method violates its general contract! 要明确返回-1, 0, ...
- 关于python 环境变量
1.默认命令行的启动的python 版本,这依赖于系统的环境变量. 见上一篇关于linux 环境变量的PATH 变量的设置 2.python 中 import 包的搜索路径, 即除了当前程序目录,能i ...
- R语言中如何使用最小二乘法
R语言中如何使用最小二乘法 这里只是介绍下R语言中如何使用最小二乘法解决一次函数的线性回归问题. 代码如下: > x<-c(6.19,2.51,7.29,7.01,5.7, ...
- 10分钟完成 mongodb replSet 部署
开始: ------------------------------------------------------------------------------------------------ ...
- SOFARPC学习(一)
接触SOFARPC,是从一个好朋友(女程序媛)的推荐开始,目的是从头到尾了解这个框架,包括使用方法和源码解析. 当学习一个新东西的事物,我总喜欢先总体把握,在深入细节,这样就可以有种高屋建瓴的感觉,否 ...
- Jeecms网站直接访问html静态页面
jeecms网站维护,遇到了直接通过链接的方式访问静态页面,jeecms官网也做了详细的解答,但是没有得到满意的结果.但是通过自己的深入研究以及别人的帮助,发现了一个很好的解决方法. 首先说明一下je ...
- hibernate 注解使用
实体类声明,需要引用 import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name=&quo ...
- Intellij IDEA 创建资源文件夹 source folder
[转载]原文链接:https://blog.csdn.net/u014723529/article/details/78050725 之前用 eclipse 新建有一个 source folder , ...