【一天一道LeetCode】#115. Distinct Subsequences
一天一道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的更多相关文章
- [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 ... 
- [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 ... 
- 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 ... 
- 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 ... 
- Leetcode 115 Distinct Subsequences 解题报告
		Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ... 
- Leetcode#115 Distinct Subsequences
		原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ... 
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ... 
- 115. Distinct Subsequences
		题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ... 
- [Leetcode][JAVA] Distinct Subsequences
		Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ... 
随机推荐
- 从零开始搭建口袋妖怪管理系统(2)-借助ngRoute实现详情页面跳转
			一.目标 上一次我们用Angular1.x完成了简单的口袋妖怪展示列表页面,现在我们想要了解口袋妖怪更多的信息,但是发现原有单行表格可能容纳不下口袋妖怪的所有信息,所以现在我们需要一个口袋妖怪详情界面 ... 
- Chinese-Text-Classification:Tensorflow CNN 模型实现的中文文本分类器[不分词版]
			从现在的结果来看,分词的版本准确率稍微高一点. 训练过程: 模型评估: 实验三,准备换一下数据集,用这里的数据集来跑这个模型:https://zhuanlan.zhihu.com/p/30736422 ... 
- 运行eclipse java virtual machine launcher 什么错误
			在MyEclipse的安装目录eclipse有个eclipse.ini文件原来的配置如下:-showsplashcom.genuitec.myeclipse.blue.product.ide--lau ... 
- STM32 基DMA的DAC波形发生器
			DAC是STM32系列的一个基本外设,可以将数字信号转化成模拟信号,这次我将使用DAC来输出一个特定波形. 首先确定工作方法,由于我目前在做的简易示波器在输出波形的同时还需要显示输入信号,所以不能占用 ... 
- tensorflow共享变量 the difference between tf.Variable() and get_variable()
			一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ... 
- 使用gogs搭建git私有仓库
			搭建gogs 我的机器环境:centos 7 1.安装git yum install git 2.安装mysql gogs的数据存在mysql中,需要安装一个mysql来存数据,当然也有其他的选择 ... 
- 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)
			1.线性规划模型: 2.使用python scipy.optimize linprog求解模型最优解: 在这里我们用到scipy中的linprog进行求解,linprog的用法见https://doc ... 
- 20160218.CCPP体系详解(0028天)
			程序片段(01):加法.c 内容概要:字符串计算表达式 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <st ... 
- Android适配难题全面总结
			支持多种屏幕 Android 可在各种具有不同屏幕尺寸和密度的设备上运行.对于 应用,Android 系统在不同设备中提供一致的开发环境, 可以处理大多数工作,将每个应用的用户界面调整为适应其显示的 ... 
- Tomcat如何实现WebSocket
			WebSocket协议属于HTML5标准,越来越多浏览器已经原生支持WebSocket,它能让客户端和服务端实现双向通信.在客户端和服务器端建立一条WebSocket连接后,服务器端消息可直接发送到客 ... 
