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的更多相关文章

  1. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  2. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  6. 【LeetCode】114. Distinct Subsequences

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  7. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  8. 【Leetcode】115. Distinct Subsequences

    Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...

  9. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 使用ss命令代替 netstat

    ss是Socket Statistics的缩写. 顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的 ...

  2. 替换jar中的指定文件

     替换jar 包下面的class 文件,很多人会想到直接用winrar 打开替换,在一般的情况下, 是可行的,但是如果说这个jar 的代码经过混淆后,会有大小写不同,文件名是相同的,在windos ...

  3. Nmap笔记本

    nmap -vv 192.168.1.100 -p1-65535 跑1-65535的端口并且设置对结果的详细输出 nmap -vv 192.168.1.1/8 -p 1433 --open 跑开放14 ...

  4. uGUI Anchor

    Anchor定位:inspector面板的Rect Transform组件中PosX左边的方框图标就是设置锚点的,做界面自适应时可定位控件在视图中的位置,与NGUI类似.Anchor+Canvas的C ...

  5. vim中ctags应用

    ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM.并且VIM中已经默认安装 ...

  6. Storm学习笔记——简介

    1. 简介 流式计算的历史 早在7.8年前诸如UC伯克利.斯坦福等大学就开始了对流式数据处理的研究,但是由于更多的关注于金融行业的业务场景或者互联网流量监控的业务场景,以及当时互联网数据场景的限制,造 ...

  7. java打印和重写toString

    class Person { private String name; public Person(String name) { this.name=name; } } public classPri ...

  8. FXGameEngine

    最初是在 http://jayskills.com/ 这个网站发现的,但一直未找到源码,今天偶然发现其源码位于 https://github.com/eppleton/FXGameEngine 下载, ...

  9. 【转】MYSQL 存储过程定时操作数据库

    这个涉及2个步骤,第一个就是建立存储过程: create procedure clear_table() begin drop database XXX end 第二步就是让其定时运行: 查看even ...

  10. 第二百二十三节,jQuery EasyUI,ComboBox(下拉列表框)组件

    jQuery EasyUI,ComboBox(下拉列表框)组件,可以远程加载数据的下拉列表组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 C ...