leetcode -day 15 Distinct Subsequences
1、
Distinct Subsequences
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的数目。如样例中的第一个为删掉第一个b,第二个为删除第二个b,第三个为删除第三个b,因此数量为3.
首先想到的方法是回溯法。可是遇到长串时超时。里面包括太多反复子问题。
代码例如以下:Time Limit Exceeded
class Solution {
public:
int numDistinct(string S, string T) {
num = 0;
if(S.length() < T.length()){
return num;
}
numDistinctCore(S,T,0,0);
return num;
}
void numDistinctCore(string& S,string& T, int si, int tj){
int slen = S.length();
int tlen = T.length();
if(slen-si < tlen -tj){
return;
}
if(tj == tlen){
++num;
}
for(int i = si; i<slen; ++i){
if(S[i] == T[tj]){
numDistinctCore(S,T,i+1, tj+1);
}
}
}
int num;
};
考虑到回溯法子问题反复求解。想到利用动态规划的方法。此题有些像求最大公共字串,可是须要改动一下,寻找子问题。
设dp[i][j]为S字符串截止到i时。能将S前面字符串能转换为T截止到j的子串的转换次数。求dp[i][j]时。一种方法转换时即删除S[i],变回S[i-1][j]的问题。还有一种方法。假设S[i] == T[j]。则转换为dp[i-1][j-1]的问题。
即为同样时为 dp[i][j]
= dp[i-1][j] + dp[i-1][j-1] 。不同一时候为 dp[i][j] = dp[i-1][j]
Accepted
class Solution {
public:
int numDistinct(string S, string T) {
int slen = S.length();
int tlen = T.length();
if(slen < tlen){
return 0;
}
int **dp = new int*[slen+1];
for(int i=0; i<slen+1; ++i){
dp[i] = new int[tlen+1];
dp[i][0] = 1;
}
for(int j=1; j<tlen+1; ++j){
dp[0][j] = 0;
}
for(int i=1; i<slen+1; ++i){
for(int j=1; j<tlen+1; ++j){
int temp = dp[i-1][j];
if(S[i-1] == T[j-1]){
temp += dp[i-1][j-1];
}
dp[i][j] = temp;
}
}
int result = dp[slen][tlen];
for(int i=0; i<slen+1; ++i){
delete[] dp[i];
}
delete[] dp;
return result;
}
};
leetcode -day 15 Distinct Subsequences的更多相关文章
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。
/** 题目:hdu6003 Problem Buyer 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6003 题意:给定n个区间,以及m个数,求从n个区 ...
- JDK7 Garbage Frist
JDK7 G1新型垃圾回收器. http://www.infoq.com/cn/articles/jdk7-garbage-first-collector
- a标签点击事件
onclick="detail(this,'${vo.id}')" function detail(obj,id){ var lb = $("#lb").val ...
- Easyui data方法扩展finder
finder: function(jq, conditions){ if(!$(jq).data("OriginalData")){ $(jq).data("Origin ...
- 读取Properties文件六种方法
1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream ...
- 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest D Dividing Marbles
题目大意: 给出一个$N(N <= 2^{22}$),$N$的二进制表示中1的个数不超过4. 一开始有一个集合$S = {N}$, 每次操作可以选择$n\in S \ (n > 1)$, ...
- JQuery基本选择器和基本动画方法总结
刚开始接触JQuery是在大三的时候,那时候先学的Javascript,然后跳跃到JQuery,就一个字,爽.但因为之前用的不是太多,所以很多都忘了,直接导致的后果就是之前在一家公司面试,面试官问我要 ...
- 卡友pos机使用流程
Q: pos机正常使用步骤 A: 1. 按开机键开机2. 输入“01”进行签到3. 系统提示输入密码,密码为“0000”4. 系统提示“请刷卡”,可正常刷卡消费首次使用请务必登陆商户后台核对结算收款账 ...
- CentOS7.1 安装Liberty之环境准备(1)
一.基础平台 1.一台装有VMware的windows系统(可联网) 2.CentOS 7.1 64bit镜像 二.最小化安装两台CentOS 7.1 的虚拟机controller.compute1, ...
- mac 干掉Dashboard
打开终端,输入下面的命令: defaults write com.apple.dashboard mcx-disabled -boolean YES 然后再重启一下 Dock,在终端输入 kill ...