【一天一道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 ...
随机推荐
- 使用VMware Converter Standalone Client进行虚拟机 P2V提示 权限不足,无法连接\\ip\admin$的解决方法集锦
使用VMware vCenter Converter Standalone Client进行虚拟机 P2V提示 权限不足,无法连接\\ip\admin$的解决方法集锦 步骤一 检查 远程桌面到&quo ...
- Linux sort -n 与 -g 排序对比
公司业务需要,天天用awk统计数值然后排序,出问题了,如下: count.sh 是一个统计脚本,把awk输出的值按占比.条数.类型 在重新输出 awk -F\| '{print $16}' *MQTT ...
- 关于 printf scanf getchar
float默认小数6位 右对齐.-m 左对齐 在调用printf函数输出数据时,当数据的实际位宽大于printf函数中的指定位宽时,将按照数据的实际位宽输出数据. .n表精度 输出%符号 注意点 #i ...
- 反射实现java深度克隆
一.克隆 有时想得到对象的一个复制品,该复制品的实体是原对象实体的克隆.复制品实体的变化不会引起原对象实体发生变化,这样的复制品称为原对象实体的克隆对象或简称克隆. 1.浅复制(浅克隆) 概念:被复制 ...
- 关于mysql安装到最后一步老是停留在starting server,显示无响应
从昨天晚上到今天安装MySQL花了好长的时间,一直是在后面starting server 这部就显示无响应,查资料了解到是MySQL有残留,有些注册表文件需要手动清理,下面是具体方法. 1.先用卸载软 ...
- python笔记四(dict/set/不可变对象)
一.dict 字典是包含key_value存储方式.在放进去的时候,必须根据key值Hash出value的存放位置,这样,取的时候才能根据key直接拿到value. dict的操作: d = {'Mi ...
- 1. 两数之和 LeetCode
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...
- CF | Alyona and Mex
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyon ...
- Zookeeper的安装部署
1.Zookeeper的安装部署 7.1 Zookeeper工作机制 7.1.1.Zookeeper集群角色 Zookeeper集群的角色: Leader 和 follower (Observer ...
- Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表
Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...