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. CentOS 7下Java的SecureRandom种子初始化失败解决办法

    io.netty.util.internal.ThreadLocalRandom getInitialSeedUniquifierWARNING: Failed to generate a seed ...

  2. 做过的自定义 View

    做过的自定义 View android view custom 音频条状图 需求 详细设计 具体实现 音频条状图 需求 音频图 最终效果类似于音频图中的条状图 只是效果模拟,并不监听真实的音频 条的宽 ...

  3. NGUI3.7.4实现循环拖动

    前段时间下了NGUI新版本3.7.4,看到例子Endless Scroll Views,实现了循环拖动,可能会用到,先把实现步骤贴出来跟大家分享一下. 1.首先新建一个背景. 2.添加所需控件,类似滑 ...

  4. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

  5. Storm学习笔记——安装配置

    1.安装一个zookeeper集群 2.上传storm的安装包,解压 3.修改配置文件conf/storm.yaml #所使用的zookeeper集群主机storm.zookeeper.servers ...

  6. shell课后总结

      shell课后总结   作者:高波 归档:学习笔记 2017年12月4日13:31:08 快捷键: Ctrl + 1 标题1 Ctrl + 2 标题2 Ctrl + 3 标题3 Ctrl + 4 ...

  7. inux redis 安装配置, 以及redis php扩展

    一,什么是redis redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset ...

  8. oralce函数

    1.trunc函数处理数字和日期TRUNC(NUMBER[,DECIMAL]) 数字格式TRUNC(DATE[,FOMAT]) 日期格式2.round函数(四舍五入)ROUND(NUMBER[,DEC ...

  9. boost::archive::binary_iarchive

    #include <iostream> #include <string> #include <sstream> #include <vector> # ...

  10. Delphi xe7 android实现透明度可以调整的对话框

    Delphi xe7 android实现透明度可以调整的对话框 Delphi xe7 android实现透明度可以调整的对话框 Delphi xe7 android实现透明度可以调整的对话框 要实现对 ...