一天一道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. java 第三次作业

    (一)学习总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Gr ...

  2. 基于GCC的openMP学习与测试

    (一).openMP简述 Open Multiprocessing (OpenMP) 框架是一种功能极为强大的规范,可以帮助您利用 C.C++ 和 Fortran 应用程序中的多个核心带来的好处,是基 ...

  3. android高德地图网络路径实现自定义marker并点击弹出自定义窗口

    android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图) 在这里我们使用Grilde去加载网络图片,因为这个简直太方便了! ...

  4. (MariaDB)开窗函数用法

    本文目录: 1.1 窗口和开窗函数简介 1.2 OVER()语法和执行位置 1.3 row_number()对分区排名 1.4 rank()和dense_rank() 1.5 percent_rank ...

  5. python之with的使用

      python之with使用                                         with工作原理            紧跟with后面的语句被求值后,返回对象的__e ...

  6. tensorflow共享变量 the difference between tf.Variable() and get_variable()

    一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...

  7. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  8. Node.js URL

    稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...

  9. Spring常用配置

    ----------------------------------------------------------------------------------------------[版权申明: ...

  10. Scikit-learn:分类classification

    http://blog.csdn.net/pipisorry/article/details/53034340 支持向量机SVM分类 svm分类有多种不同的算法.SVM是非常流行的机器学习算法,主要用 ...