leetcode -day 15 Distinct Subsequences
1、
Distinct Subsequences
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.
分析:此题乍一看看不明确啥意思,后面慢慢理解。就是说T是S的子串,这里的子串是说原字符串删除某些字符后剩余字符的组合,题目是S中通过删除操作获得子串T的数目。如样例中的第一个为删掉第一个b,第二个为删除第二个b,第三个为删除第三个b,因此数量为3.
首先想到的方法是回溯法。可是遇到长串时超时。里面包括太多反复子问题。
代码例如以下:Time Limit Exceeded
class Solution {
public:
int numDistinct(string S, string T) {
num = 0;
if(S.length() < T.length()){
return num;
}
numDistinctCore(S,T,0,0);
return num;
}
void numDistinctCore(string& S,string& T, int si, int tj){
int slen = S.length();
int tlen = T.length();
if(slen-si < tlen -tj){
return;
}
if(tj == tlen){
++num;
}
for(int i = si; i<slen; ++i){
if(S[i] == T[tj]){
numDistinctCore(S,T,i+1, tj+1);
}
}
}
int num;
};
考虑到回溯法子问题反复求解。想到利用动态规划的方法。此题有些像求最大公共字串,可是须要改动一下,寻找子问题。
设dp[i][j]为S字符串截止到i时。能将S前面字符串能转换为T截止到j的子串的转换次数。求dp[i][j]时。一种方法转换时即删除S[i],变回S[i-1][j]的问题。还有一种方法。假设S[i] == T[j]。则转换为dp[i-1][j-1]的问题。
即为同样时为 dp[i][j]
= dp[i-1][j] + dp[i-1][j-1] 。不同一时候为 dp[i][j] = dp[i-1][j]
Accepted
class Solution {
public:
int numDistinct(string S, string T) {
int slen = S.length();
int tlen = T.length();
if(slen < tlen){
return 0;
}
int **dp = new int*[slen+1];
for(int i=0; i<slen+1; ++i){
dp[i] = new int[tlen+1];
dp[i][0] = 1;
}
for(int j=1; j<tlen+1; ++j){
dp[0][j] = 0;
}
for(int i=1; i<slen+1; ++i){
for(int j=1; j<tlen+1; ++j){
int temp = dp[i-1][j];
if(S[i-1] == T[j-1]){
temp += dp[i-1][j-1];
}
dp[i][j] = temp;
}
}
int result = dp[slen][tlen];
for(int i=0; i<slen+1; ++i){
delete[] dp[i];
}
delete[] dp;
return result;
}
};
leetcode -day 15 Distinct Subsequences的更多相关文章
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- linux 查看java进程
ps -ef|grep java 1. jps 或 ps –ef|grepjava (获取PID)
- 在Windows下使用nmake+Makefile+编译ZThread库(附例子)
----------2015/01/09/23:21更新----------------------------------- 关于保留DEBUG信息的一个简单例子,见这篇随笔 ----------2 ...
- shell 实现mysql写入操作
mysql -uroot study -proot << EOF > insert into top_n_movie(movie,sumprice)values('hello kit ...
- MySQL定义异常和异常处理方法
在MySQL中.特定异常须要特定处理.这些异常可以联系到错误,以及子程序中的一般流程控制.定义异常是事先定义程序运行过程中遇到的问题,异常处理定义了在遇到问题时相应当採取的处理方式.而且保证存储过程或 ...
- deep learning+ Depth Estimation
Depth estimation/stereo matching/optical flow @CVPR 2017 Unsupervised Learning of Depth and Ego-Moti ...
- 第一百四十五节,JavaScript,同步动画
JavaScript,同步动画 将上一节的,移动透明动画,修改成可以支持同步动画,也就是可以给这个动画方法多个动画任务,让它同时完成 原理: 向方法里添加一个属性,这个属性是一个对象,同步动画属性,属 ...
- VS2010之– Web Development(四)-将WebApplication打包发布到IIS
下面将介绍怎样将一个WebApplication在VisualStudio中直接打包发布到IIS上去. 首先需要使用Administrator的身份运行Visual Studio. Task 1 – ...
- django form POST方法提交表达
之前就着手开始尝试用django来简化web开发的流程周期,果不其然,速度还行,当然前期的产品那就相当粗糙了.举例来说,就连最基本的登录都是抄别人的,最可怕的是用GET方法提交表单,今天就尝试解决这个 ...
- HTML特殊字符的html、js、css写法汇总
⇠ 箭头类 符号 UNICODE 符号 UNICODE HTML JS CSS HTML JS CSS ⇠ ⇠ \u21E0 \21E0 ⇢ ⇢ \u21E2 \2 ...
- html5 webwork
在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了. 一个浏览器至少存在三个线程:js引擎线程(处理js).GUI渲 ...