【一天一道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 ...
随机推荐
- 8.QT-对话框(模态与非模态)
对话框介绍 对话框是于用户进行简易交互的顶层窗口 QDialog是Qt中所有对话框窗口的父类,是一种容器类型的组件 QDialog继承于QWidget类,如下图所示: QWidget和QDialog有 ...
- iframe嵌套页面 音频在微信公众号环境无法播放
在微信公众号中 没有iframe的时候window.WeixinJSBridge为对象,有iframe时为undefined 要使用 window.parent.WeixinJSBridge得到 if ...
- easyui datagrid 动态表头2
前端 function goqry() { $("#form").form("submit", { url: "CJCK_bjcjfx_yfsl.as ...
- RedisAsyncClientAdapter-------------接口继承
public abstract class RedisAsyncClientAdapter<K, V, T extends RedisKeyAsyncCommands<K, V> & ...
- php中AJAX请求中使用post和get请求的区别
之前使用这两个请求的时候,主要从几个方面考虑: 1.语义,get就是从服务器获取,一般就是获取/查询资源信息.post就是提交给服务器,一般就是更新资源信息. 2.请求文件大小,get一般只有2k这样 ...
- C# TextBox 焦点
TextBox焦点问题 1.失焦 KeyBoard.ClearFocus(); 存在一个问题,失去焦点之后,中文通过输入法依旧是可以输入的. 如果是中文文本框,按Enter失焦,同时禁止输入中文,可以 ...
- java 需要准备的知识(转摘)
需要准备的知识 以下为在近期面试中比较有印象的问题,也就不分公司了,因为没什么意义,大致分类记录一下,目前只想起这么多,不过一定要知道这些问题只是冰山一角,就算都会了也不能怎么样,最最重要的,还是坚实 ...
- python--ftp服务器(pyftpdlib)
# -*- coding: utf-8 -*-# @Time : 2018/4/11 16:47# @Author : liuxiaobing# @File : test2.py# @Software ...
- /bin/bash 常用命令
ls -a 查看一切文件 ls -l 查看目录文件信息 clear 清屏(信息保留屏幕) reset 清屏(所有信息) ls *.txt 查看所有txt文件 wc 统计文件 -l 行数: - c 统计 ...
- SQL实例整理
本文适合将w3school的SQL教程(http://www.w3school.com.cn/sql/sql_create_table.asp)都基本看过一遍的猿友阅读. 说说博主的情况吧.毕业找工作 ...