一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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相等。

解题思路:S的子串代表在S中删除一些字符组成的字符串,那么可以很容易想到用递归来解决。

如果当前s[i]==s[j],有两种情况,选择该字母(i++,j++)和跳过该字母(i++)

如果s[i]!=s[j],则直接i++.

具体见代码:

class Solution {
public:
    int numDistinct(string s, string t) {
        if(s.length()==0||t.length()==0) return 0;
        int num =0;
        dpDistinct(s,t,0,0,num);
        return num;
    }
    void dpDistinct(string& s, string& t , int i , int j,int& num)
    {
        if(j==t.length()){num++;return;}
        if(s[i]==t[j])
        {
            //choose this words
            if(i<s.length()&&j<t.length()) dpDistinct(s, t , i+1, j+1,num);
            //not choose this words
            if(i<s.length()) dpDistinct(s, t , i+1, j,num);
        }
        else
            //Don't equal
            if(i<s.length()) dpDistinct(s, t , i+1, j,num);
    }
};

然后……直接超时了。

观察上述代码,发现有很多重复的判断,因此,可以采用动态规划的思想。

开始找状态转移方程。dp[i][j]用来表示s中j之前的子串subs中有多少个不同的subt,其中subt为t中i之前的字符组成的子串。

首先,初始化dp,令dp[0][j]都等于1,因为s中删除所有的字符都能组成空串。

如果t[i] == s[j],那么dp[i][j] = dp[i][j-1]+dp[i-1][j-1]

否则,dp[i][j] = dp[i][j-1]

举例说明一下:s为abbbc,t为abc

dp a b b b c
1 1 1 1 1 1
a 0 1 1 1 1 1
b 0 0 1 2 3 3
c 0 0 0 0 0 3

具体解释看代码:

class Solution {
public:
    int numDistinct(string s, string t) {
        vector<vector<int>> dp;
        for(int i = 0 ; i < t.length()+1;i++)
        {
            vector<int> temp(s.length()+1,0);
            dp.push_back(temp);
        }
        for(int i = 0 ; i < s.length()+1;i++) dp[0][i]=1;//初始化dp
        for(int i = 1 ; i < t.length()+1; i++)
        {
            for(int j = 1 ; j < s.length()+1 ; j++)
            {
                if(s[j-1]==t[i-1])
                {
                    dp[i][j] = dp[i-1][j-1]+dp[i][j-1];//状态转移方程
                }
                else
                    dp[i][j] = dp[i][j-1];
            }
        }
        return dp[t.length()][s.length()];//返回
    }
};

【一天一道LeetCode】#115. Distinct Subsequences的更多相关文章

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

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  2. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  3. Java for LeetCode 115 Distinct Subsequences【HARD】

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

  4. leetcode 115 Distinct Subsequences ----- java

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

  5. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  6. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

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

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

  8. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  9. [Leetcode][JAVA] Distinct Subsequences

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

随机推荐

  1. Chinese-Text-Classification:Tensorflow CNN 模型实现的中文文本分类器[不分词版]

    从现在的结果来看,分词的版本准确率稍微高一点. 训练过程: 模型评估: 实验三,准备换一下数据集,用这里的数据集来跑这个模型:https://zhuanlan.zhihu.com/p/30736422 ...

  2. drools 手动创建kmoudle.xml文件

    @Test public void test() { KieServices kieServices = KieServices.Factory.get(); KieResources resourc ...

  3. 使用webpack-dev-server设置反向代理解决前端跨域问题

    webpack-dev-server是一个小型的Node.js Express服务器,它使用webpack-dev-middleware来服务于webpack的包,除此自外,它还有一个通过Sock.j ...

  4. Java 的异常处理机制

    异常是日常开发中大家都「敬而远之」的一个东西,但实际上几乎每种高级程序设计语言都有自己的异常处理机制,因为无论你是多么厉害的程序员,都不可避免的出错,换句话说:你再牛逼,你也有写出 Bug 的时候. ...

  5. vue--"卡片层叠" 组件 开发小记

    背景:影城移动点餐web App增加会员卡支付功能 需求:确认订单页点击会员卡项弹出会员卡列表,多张会员卡依次叠加覆盖上一张80%的高度,点击任意卡片则改卡片置为当前卡片,只有当前卡片显示全部卡片信息 ...

  6. C++笔记010:C++对C的扩展——register关键字增强

    register关键字:请求编译器让变量直接放到CPU内部寄存器里面,而不是通过内存寻址访问,速度快. 在C语言中,register修饰的变量不能取地址,去寄存器变量的地址在C语言里面是会出错的. i ...

  7. JavaScript原型与原型链

    一.数据类型 JavaScript的数据类型可以分为基本数据类型和引用数据类型. 基本数据类型(6种) String Number Boolean null undefined Symbol(ES6) ...

  8. Node.js Buffer(缓冲区)

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...

  9. Python 元组内置函数

    Python元组包含了以下内置函数 序号 方法及描述 1 cmp(tuple1, tuple2)比较两个元组元素. 2 len(tuple)计算元组元素个数. 3 max(tuple)返回元组中元素最 ...

  10. Java语言程序设计-Markdown格式作业模板

    Markdown格式作业模板如下,大家可以直接复制粘贴使用. 注意:作业中不能只写答案,题目本身也要出现.. # 1. 本章学习总结 你对于本章知识的学习总结 # 2. 书面作业 **Q1 java ...