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 ...
随机推荐
- 在Ubuntu Server 14.04上源码安装Odoo 9.0
1. 更新Ubuntu服务器软件源 sudo apt-get update #更新软件源 sudo apt-get dist-upgrade #更新软件包,自动查找依赖关系 sudo shutdown ...
- SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...
- Opencv中的阈值函数
OpenCV基础——threshold函数的使用 图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果. 参数原型 参数说明 src:源图像,可以为8位的灰度 ...
- linux和window双系统下修改系统启动项
参考:http://jingyan.baidu.com/article/63acb44ae4062c61fcc17e27.html: 我们在安装双系统之后经常会遇到想打开windows但默认启动项是u ...
- HTTP协议详解(经典)
转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的 ...
- [转]web计时机制——performance对象
页面性能一直都是Web开发人员比较关注的领域.但在实际应用中,度量页面性能的指标,是javascript的Date对象.Web Timing API改变了这个局面,让开发人员通过javascript就 ...
- org.hibernate.service.ServiceRegistryBuilder被弃用
看视频教程是这样写的: //创建配置对象 Configuration config = new Configuration().configure(); //创建服务注册对象 ServiceRegis ...
- tinkcmf视频上传大小限制
/application/Common/Common/function.php 找到upload_max_filesize把后面的数值改成合适的大小(单位是KB)
- python枚举详解
1. 枚举的定义 首先,定义枚举要导入enum模块. 枚举定义用class关键字,继承Enum类. 用于定义枚举的class和定义类的class是有区别[下一篇博文继续分享]. 示例代码: from ...
- 【扩展推荐】Laravel-ide-helper 高效的 IDE 智能提示插件 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 - Powered by PHPH
说明# barryvdh/laravel-ide-helper 扩展包能让你的 IDE ( PHPStorm, Sublime ) 实现自动完成.代码智能提示和代码跟踪等功能,大大提高你的开发效率. ...