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 ...
随机推荐
- TZOJ 5094 Stringsobits(DP)
描述 Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either ...
- JavaScript中this的指向2(转载)
1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...
- input输入框的input事件和change事件
input输入框的onchange事件,要在 input 失去焦点的时候才会触发: 在输入框内容变化的时候不会触发change,当鼠标在其他地方点一下才会触发: onchange 事件也可用于单选框与 ...
- OpenLayers在多个矢量图层编辑要素
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- Javaweb Form表单查询
1.表单(form),是一种可以由用户输入,并提交给服务器端的一个图形界面,有如下性质: (1)表单中可以输入一些内容,这些输入功能由控件提供,叫做表单元素 (2)表单中一般都有一个按钮负责提交 (3 ...
- Asp.Net Core2.0在linux下发布
一.在linux上新建mvc项目发布 可以参考:https://segmentfault.com/a/1190000012428781 也可以看微软官方文档. 大致步骤如下: 1.在linux下安装. ...
- java swing多线程
比如一个爬虫 在界面上显示当前时间,每秒都刷新一次用来判断软件是不是卡死 在爬取程序运行的时候,界面可能会卡死 那这就要把爬取程序放在另一个线程里边 同时,也可以把rtc放在另一个线程里边 具体代码, ...
- Jboss配置HTTPS
配置jboss的HTTP请求走SSL(HTTPS协议) l 生成keystore 文件 用keytool生成server.keystore文件: 进入命令行 C:\Documents ...
- hadoop-hive查询ncdc天气数据实例
使用hive查询ncdc天气数据 在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果. 1. 在hive中创建ncdc表,这个 ...
- 洛谷P1082 同余方程 [2012NOIP提高组D2T1] [2017年6月计划 数论06]
P1082 同余方程 题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输 ...