由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问道leetcode上的题:distinct subsequence
相关主题
leetcode一题没看明白那个leetcode上头得distinct subsequence什么意思
Question on leetcode Distinct Subsequences一个答案看不明白谁解释一下
大家帮忙解释一个 LeetCode DP (distinct subsequences)专家们,find the longest common substring of two strings
Distinct Subsequence求助:leetcode上的Distinct Subsequences这个怎么理解
leetcode115求帮理解 LeetCode 上的Distinct Subsequences这道题究竟是什么意思???
求解这个动态规划题DP的状态转移方程
请教道算法题贴一下我google第一轮店面的题目
问一道算法题max length of subsequence string matching subsBloomberg面试题
相关话题的讨论汇总
话题: dp话题: string话题: int话题: return
进入JobHunting版参与讨论
1 (共1页)
I***C
发帖数: 765
1
我用递归做的,小的test case都过了,大的test case运行时间过长。返不回答案,
哪位做过分享一下经验吧,谢谢
---------------------
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.
---------------------
method interface:
int numDistinct(string S, string T)
--------------------
Test cases
"
aabdbaabeeadcbbdedacbbeecbabebaeeecaeabaedadcbdbcdaabebdadbbaeabdadeaabbabbe
cebbebcaddaacccebeaeedababedeacdeaaaeeaecbe", "bddabdcae" 10582116
"
daacaedaceacabbaabdccdaaeaebacddadcaeaacadbceaecddecdeedcebcdacdaebccdeebcbd
eaccabcecbeeaadbccbaeccbbdaeadecabbbedceaddcdeabbcdaeadcddedddcececbeeabcbec
aeadddeddccbdbcdcbceabcacddbbcedebbcaccac", "ceadbaa" 8556153
"
babeceeacaadababaceacbcbdaeddedbbccddbdadaedbbccdbcaebecdacdedaaccaabecbbdcd
eececbcbebacbebdcbdbaebbabadbcdbbacddebcabaecdccecdbdcaddacecacada", "adebd"
262228
"
adbdadeecadeadeccaeaabdabdbcdabddddabcaaadbabaaedeeddeaeebcdeabcaaaeeaeeabcd
dcebddebeebedaecccbdcbcedbdaeaedcdebeecdaaedaacadbdccabddaddacdddc", "
bcddceeeebecbc" 700531452
"
acdbccddceaaeaeacdacebbadacbacaccdabceeedcbccbecbeecbacbcceceebddcaabbcbddab
bededebbebcbdebedcddbabcbddcdddccdddcabddebbdecdcccacadbddddcdcbdbbaddabebee
bbcaebabbaebecabcceaabcdbceabde", "eadbec" 1887265
"
deccdbebedabedecedebeccdebbaddddecacdbdeaabebcbaaccaaeabcccccadbeaaecaecacdb
ebeeedbeeecedebcbeaaaaaecbbcdebeacabccabddadeecbacbcebbbceacddbbaccebabbadeb
ebcaaececbccac", "bbbdedc" 2081338
"
ddbeabacbdbddcaecdbeeaaabaecccaaddbdebccbbaeddaabbbccecaebccbeeecbeeeedbabca
edbcecadbeedddaeabdeeedea", "ecaaebeeedbba" 7259139
"
aaddbacbbcabdbceaeeaacbabbbaccacaacbabeddbedcdceceeabccabdadccceebcbcbecdbac
edcecdeadbaebceaedaaecbbebdecabbddccebaccabaaabbabceddceecadcccdceabbcdadbba
debdadeacbaddccdeebcddaebbcbedbd", "ccdeddeabb" 527764581
"
eacabdeadcbbddccdaccadddbaaebadcbaaedeeebdabbaeccdbcbaceaceddcdbddadecebaacd
cdaeeccaebaeebceaaaaaceaedd", "baaacbceabba" 1293119
"
ceddeeebbeceaeabcedeedabccdaaaecaddbceeeabadccbebeeacbeeeeeeebdbededeeeccbaa
edeacadccedbaacbbcaadeaedcbddddcbeaeaadcebabbeccdcebccbceeaedaee", "aeacbde"
2543265
"
xslledayhxhadmctrliaxqpokyezcfhzaskeykchkmhpyjipxtsuljkwkovmvelvwxzwieeuqnjo
zrfwmzsylcwvsthnxujvrkszqwtglewkycikdaiocglwzukwovsghkhyidevhbgffoqkpabthmqi
hcfxxzdejletqjoxmwftlxfcxgxgvpperwbqvhxgsbbkmphyomtbjzdjhcrcsggleiczpbfjcgtp
ycpmrjnckslrwduqlccqmgrdhxolfjafmsrfdghnatexyanldrdpxvvgujsztuffoymrfteholgo
nuaqndinadtumnuhkboyzaqguwqijwxxszngextfcozpetyownmyneehdwqmtpjloztswmzzdzqh
uoxrblppqvyvsqhnhryvqsqogpnlqfulurexdtovqpqkfxxnqykgscxaskmksivoazlducanrqxy
nxlgvwonalpsyddqmaemcrrwvrjmjjnygyebwtqxehrclwsxzylbqexnxjcgspeynlbmetlkacnn
bhmaizbadynajpibepbuacggxrqavfnwpcwxbzxfymhjcslghmajrirqzjqxpgtgisfjreqrqabs
sobbadmtmdknmakdigjqyqcruujlwmfoagrckdwyiglviyyrekjealvvigiesnvuumxgsveadrxl
pwetioxibtdjblowblqvzpbrmhupyrdophjxvhgzclidzybajuxllacyhyphssvhcffxonysahvz
hzbttyeeyiefhunbokiqrpqfcoxdxvefugapeevdoakxwzykmhbdytjbhigffkmbqmqxsoaiomgm
mgwapzdosorcxxhejvgajyzdmzlcntqbapbpofdjtulstuzdrffafedufqwsknumcxbschdybosx
krabyfdejgyozwillcxpcaiehlelczioskqtptzaczobvyojdlyflilvwqgyrqmjaeepydrcchfy
ftjighntqzoo", "rwmimatmhydhbujebqehjprrwfkoebcxxqfktayaaeheys" 543744000
z**q
发帖数: 41
2
正着写Dp就过了, 明天有面试,求Bless
I***C
发帖数: 765
3
我用递归做的,小的test case都过了,大的test case运行时间过长。返不回答案,
哪位做过分享一下经验吧,谢谢
---------------------
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.
---------------------
method interface:
int numDistinct(string S, string T)
--------------------
Test cases
"
aabdbaabeeadcbbdedacbbeecbabebaeeecaeabaedadcbdbcdaabebdadbbaeabdadeaabbabbe
cebbebcaddaacccebeaeedababedeacdeaaaeeaecbe", "bddabdcae" 10582116
"
daacaedaceacabbaabdccdaaeaebacddadcaeaacadbceaecddecdeedcebcdacdaebccdeebcbd
eaccabcecbeeaadbccbaeccbbdaeadecabbbedceaddcdeabbcdaeadcddedddcececbeeabcbec
aeadddeddccbdbcdcbceabcacddbbcedebbcaccac", "ceadbaa" 8556153
"
babeceeacaadababaceacbcbdaeddedbbccddbdadaedbbccdbcaebecdacdedaaccaabecbbdcd
eececbcbebacbebdcbdbaebbabadbcdbbacddebcabaecdccecdbdcaddacecacada", "adebd"
262228
"
adbdadeecadeadeccaeaabdabdbcdabddddabcaaadbabaaedeeddeaeebcdeabcaaaeeaeeabcd
dcebddebeebedaecccbdcbcedbdaeaedcdebeecdaaedaacadbdccabddaddacdddc", "
bcddceeeebecbc" 700531452
"
acdbccddceaaeaeacdacebbadacbacaccdabceeedcbccbecbeecbacbcceceebddcaabbcbddab
bededebbebcbdebedcddbabcbddcdddccdddcabddebbdecdcccacadbddddcdcbdbbaddabebee
bbcaebabbaebecabcceaabcdbceabde", "eadbec" 1887265
"
deccdbebedabedecedebeccdebbaddddecacdbdeaabebcbaaccaaeabcccccadbeaaecaecacdb
ebeeedbeeecedebcbeaaaaaecbbcdebeacabccabddadeecbacbcebbbceacddbbaccebabbadeb
ebcaaececbccac", "bbbdedc" 2081338
"
ddbeabacbdbddcaecdbeeaaabaecccaaddbdebccbbaeddaabbbccecaebccbeeecbeeeedbabca
edbcecadbeedddaeabdeeedea", "ecaaebeeedbba" 7259139
"
aaddbacbbcabdbceaeeaacbabbbaccacaacbabeddbedcdceceeabccabdadccceebcbcbecdbac
edcecdeadbaebceaedaaecbbebdecabbddccebaccabaaabbabceddceecadcccdceabbcdadbba
debdadeacbaddccdeebcddaebbcbedbd", "ccdeddeabb" 527764581
"
eacabdeadcbbddccdaccadddbaaebadcbaaedeeebdabbaeccdbcbaceaceddcdbddadecebaacd
cdaeeccaebaeebceaaaaaceaedd", "baaacbceabba" 1293119
"
ceddeeebbeceaeabcedeedabccdaaaecaddbceeeabadccbebeeacbeeeeeeebdbededeeeccbaa
edeacadccedbaacbbcaadeaedcbddddcbeaeaadcebabbeccdcebccbceeaedaee", "aeacbde"
2543265
"
xslledayhxhadmctrliaxqpokyezcfhzaskeykchkmhpyjipxtsuljkwkovmvelvwxzwieeuqnjo
zrfwmzsylcwvsthnxujvrkszqwtglewkycikdaiocglwzukwovsghkhyidevhbgffoqkpabthmqi
hcfxxzdejletqjoxmwftlxfcxgxgvpperwbqvhxgsbbkmphyomtbjzdjhcrcsggleiczpbfjcgtp
ycpmrjnckslrwduqlccqmgrdhxolfjafmsrfdghnatexyanldrdpxvvgujsztuffoymrfteholgo
nuaqndinadtumnuhkboyzaqguwqijwxxszngextfcozpetyownmyneehdwqmtpjloztswmzzdzqh
uoxrblppqvyvsqhnhryvqsqogpnlqfulurexdtovqpqkfxxnqykgscxaskmksivoazlducanrqxy
nxlgvwonalpsyddqmaemcrrwvrjmjjnygyebwtqxehrclwsxzylbqexnxjcgspeynlbmetlkacnn
bhmaizbadynajpibepbuacggxrqavfnwpcwxbzxfymhjcslghmajrirqzjqxpgtgisfjreqrqabs
sobbadmtmdknmakdigjqyqcruujlwmfoagrckdwyiglviyyrekjealvvigiesnvuumxgsveadrxl
pwetioxibtdjblowblqvzpbrmhupyrdophjxvhgzclidzybajuxllacyhyphssvhcffxonysahvz
hzbttyeeyiefhunbokiqrpqfcoxdxvefugapeevdoakxwzykmhbdytjbhigffkmbqmqxsoaiomgm
mgwapzdosorcxxhejvgajyzdmzlcntqbapbpofdjtulstuzdrffafedufqwsknumcxbschdybosx
krabyfdejgyozwillcxpcaiehlelczioskqtptzaczobvyojdlyflilvwqgyrqmjaeepydrcchfy
ftjighntqzoo", "rwmimatmhydhbujebqehjprrwfkoebcxxqfktayaaeheys" 543744000
z**q
发帖数: 41
4
正着写Dp就过了, 明天有面试,求Bless
l*****a
发帖数: 559
5
递归版本的是这个吧。如果写得出递归的,dp版本的也该看得懂了。
int numDistinct(string S, string T) {
if(S.length() == 0)
return 0;
if(T.length() == 0)
return 1;

if(S.length() == 1 && T.length() == 1){
if(S[0] == T[0]) return 1;
else return 0;
}

if(S[0] != T[0]){
return numDistinct(S.substr(1), T);
}else{
return numDistinct(S.substr(1), T) + numDistinct(S.substr(1), T.
substr(1));
}
}
p********2
发帖数: 123
6
OJ过了,字符串太长也可以用bigint
public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
if(S.length()==0) return 0;
if(T.length()==0) return 1;

int[][] DP=new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++){
DP[i][0]=0;
}
for(int i=0;i<=S.length();i++){
DP[0][i]=1;
}
for(int i=1;i<=T.length();i++){
for(int j=1;j<=S.length();j++){

if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
}
}
return DP[T.length()][S.length()];
}
}
C***U
发帖数: 2406
7
你这楼好歪 哈哈

【在 z**q 的大作中提到】
: 正着写Dp就过了, 明天有面试,求Bless
s***u
发帖数: 101
8
DP的
int numDistinct(string S, string T) {
assert(T.size()>0);
if (!S.size()) return 0;
int n = S.size();
int m = T.size();
vector dp(m, 0);
for (int i=0; i for (int j=m-1; j>=0; --j) {
if (T[j] == S[i]) {
if (j==0) dp[0]++;
else dp[j]+=dp[j-1];
}
}
}
return dp[m-1];
}
f*********m
发帖数: 726
9
这步怎么理解?
if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
谢谢

【在 p********2 的大作中提到】
: OJ过了,字符串太长也可以用bigint
: public class Solution {
: public int numDistinct(String S, String T) {
: // Start typing your Java solution below
: // DO NOT write main() function
: if(S.length()==0) return 0;
: if(T.length()==0) return 1;
:
: int[][] DP=new int[T.length()+1][S.length()+1];
: for(int i=0;i<=T.length();i++){

f*********m
发帖数: 726
10
顶问题,期待回答,谢谢。
相关主题
求解这个动态规划题那个leetcode上头得distinct subsequence什么意思
请教道算法题一个答案看不明白谁解释一下
问一道算法题max length of subsequence string matching subs专家们,find the longest common substring of two strings
进入JobHunting版参与讨论
l****o
发帖数: 315
11
如果当前字符相同,那有两种可能,用到和不用到这个字符,所以这两种情况加起来。
如果不同,那只能等于用不了这个字符的情况。

【在 f*********m 的大作中提到】
: 顶问题,期待回答,谢谢。
l*****a
发帖数: 559
12
递归版本的是这个吧。如果写得出递归的,dp版本的也该看得懂了。
int numDistinct(string S, string T) {
if(S.length() == 0)
return 0;
if(T.length() == 0)
return 1;

if(S.length() == 1 && T.length() == 1){
if(S[0] == T[0]) return 1;
else return 0;
}

if(S[0] != T[0]){
return numDistinct(S.substr(1), T);
}else{
return numDistinct(S.substr(1), T) + numDistinct(S.substr(1), T.
substr(1));
}
}
p********2
发帖数: 123
13
OJ过了,字符串太长也可以用bigint
public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
if(S.length()==0) return 0;
if(T.length()==0) return 1;

int[][] DP=new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++){
DP[i][0]=0;
}
for(int i=0;i<=S.length();i++){
DP[0][i]=1;
}
for(int i=1;i<=T.length();i++){
for(int j=1;j<=S.length();j++){

if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
}
}
return DP[T.length()][S.length()];
}
}
C***U
发帖数: 2406
14
你这楼好歪 哈哈

【在 z**q 的大作中提到】
: 正着写Dp就过了, 明天有面试,求Bless
s***u
发帖数: 101
15
DP的
int numDistinct(string S, string T) {
assert(T.size()>0);
if (!S.size()) return 0;
int n = S.size();
int m = T.size();
vector dp(m, 0);
for (int i=0; i for (int j=m-1; j>=0; --j) {
if (T[j] == S[i]) {
if (j==0) dp[0]++;
else dp[j]+=dp[j-1];
}
}
}
return dp[m-1];
}
f*********m
发帖数: 726
16
这步怎么理解?
if(S.charAt(j-1)==T.charAt(i-1)){
DP[i][j]=DP[i][j-1]+DP[i-1][j-1];
}
else
DP[i][j]=DP[i][j-1];
谢谢

【在 p********2 的大作中提到】
: OJ过了,字符串太长也可以用bigint
: public class Solution {
: public int numDistinct(String S, String T) {
: // Start typing your Java solution below
: // DO NOT write main() function
: if(S.length()==0) return 0;
: if(T.length()==0) return 1;
:
: int[][] DP=new int[T.length()+1][S.length()+1];
: for(int i=0;i<=T.length();i++){

f*********m
发帖数: 726
17
顶问题,期待回答,谢谢。
l****o
发帖数: 315
18
如果当前字符相同,那有两种可能,用到和不用到这个字符,所以这两种情况加起来。
如果不同,那只能等于用不了这个字符的情况。

【在 f*********m 的大作中提到】
: 顶问题,期待回答,谢谢。
l*******0
发帖数: 63
19
哎,DP题目,总是每次想不出来。。。但是看过各位高手的思路后,又觉得懂了。。真
是弱爆了。。。
1 (共1页)
进入JobHunting版参与讨论
相关主题
Bloomberg面试题leetcode115
分享Imo电面题求解这个动态规划题
这段LIS为啥崩溃?请教道算法题
有人同看Longest Palindromic Substring 这道题么?问一道算法题max length of subsequence string matching subs
leetcode一题没看明白那个leetcode上头得distinct subsequence什么意思
Question on leetcode Distinct Subsequences一个答案看不明白谁解释一下
大家帮忙解释一个 LeetCode DP (distinct subsequences)专家们,find the longest common substring of two strings
Distinct Subsequence求助:leetcode上的Distinct Subsequences这个怎么理解
相关话题的讨论汇总
话题: dp话题: string话题: int话题: return