一天一道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. 在 Mac OS X 上安装 TensorFlow

    在 Mac OS X 上安装 TensorFlow 这个文档说明了如何在 Mac OS X 上安装 TensorFlow. 注意:从 1.2 版本开始,在 Mac OS X 上 TensorFlow ...

  2. Luogu P2756 [网络流24题]飞行员配对方案问题_二分图匹配

    二分图模板题 我用的是匈牙利 其实最大流也可以做 #include<iostream> #include<cstdio> #include<cstdlib> #in ...

  3. plsql和tsql常用函数比较

    数学函数 .绝对值 S:) value O:) value from dual .取整(大) S:select ceiling(-1.001) value O:select ceil(-1.001) ...

  4. 剑指架构师系列-MySQL调优

    介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...

  5. Docker其它安全特性

    除了能力机制之外,还可以利用一些现有的安全机制来增强使用 Docker 的安全性,例如 TOMOYO, AppArmor, SELinux, GRSEC 等. Docker 当前默认只启用了能力机制. ...

  6. Linux常见目录及命令介绍

    一.Linux中常用的目录介绍:     /        -根目录     /bin    -命令保存目录(普通用户亦可读取的命令)     /boot    -启动目录,启动相关文件     /d ...

  7. 毕业论文内容框架指导-适用于MIS系统

    摘要: 背景.要做什么.选用什么技术.按照什么过程.原理.或者步骤去做.最后做出了什么东西.做出来的东西有什么用. 1. 前言 系统的背景与意义:为什么要做这个系统 ? 现状调查:别人做的怎么样? 系 ...

  8. iOS中的颜色

    最近在改Bug的时候,才注意到iOS 中的颜色竟然也大有文章,特来记录一下. 先说一下问题,因为某界面中有用xib实现的一个view,而这个view 只在UIColletionView的layout ...

  9. 代理IP爬取,计算,发放自动化系统

    IoC Python端 MySQL端 PHP端 怎么使用 这学期有一门课叫<物联网与云计算>,于是我就做了一个大作业,实现的是对代理IP的爬取,计算推荐,发放给用户等任务的的自动化系统.由 ...

  10. Windows 为右键菜单瘦身

    当你想删除右键菜单中某些选项时,一种比较合适的思路是: 1.如果软件本身提供了控制选项,那么直接在该软件设置即可.没必要在注册表操作.比如360安全卫士和360杀毒都提供了这种机制. 值得一提的是,3 ...