一天一道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. python 程序中调用go

    虽然python优点很多,但是有一个致命的缺点就是运行速度太慢,那么python程序需要一些计算量比较大的模块时一般会调用c或者c++的代码来重写,但是c/c++编写代码代价太高,耗费太多的人力.那么 ...

  2. java-反射深度剖析

    Java反射是Java语言一个很重要的特征,简单剖析下反射的定义.原理.使用.性能及应用场景. (一)定义 程序运行时,允许改动程序结构或变量类型,这种语言称为动态语言.java不属于动态语言,但提供 ...

  3. hibernate实体对象的三种状态:自由状态,持久状态,游离状态.

    自由态与游离态的区别: 当一个持久化对象,脱离开Hibernate的缓存管理后,它就处于游离状态,游离对象和自由对象的最大区别在于,游离对象在数据库中可能还存在一条与它 对应的记录,只是现在这个游离对 ...

  4. 补充Mysql5.7用法

    下面简单介绍一下安装: [root@MySQL soft]# tar xf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz -C /data/service/ [r ...

  5. AnyConnect使用说明(手机版)

    一.下载安装客户端 iPhone手机在App Store 里搜索 “Anyconnect”下载安装. Android手机需另外下载Anyconnect. 二. 1.打开AnyConnect,点击&qu ...

  6. ACM 饭卡

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  7. Dynamics CRM2016 Web Api之查询查找字段的相关属性

    之前有篇博文介绍了如何获取查找字段的name值(跳转),本篇在此基础上再延伸下,实现的效果类似于EntityReference,可以取到查找字段的id,name,localname. 这里我以客户实体 ...

  8. Rails rspec测试报patch user_path(user) param not found: user的解决

    其实道理很简单,就是在User控制器的update方法中有一个验证: def user_params params.require(:user).permit(:name,:email,:passwo ...

  9. Vulkan的分层设计

    Vulkan驱动层提供了简单高效的API.作为Vulkan API的使用者,我们要严格遵循Vulkan API的使用规则.如果我们违反了这些规则,Vulkan只会返回很少的反馈,它只会报告一部分严重和 ...

  10. 将树形递归转换为loop

    class Stack(object): def __init__(self,**kwargs): self.__dict__.update(kwargs) def __str__(self): re ...