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.

  

class Solution {
public: void DFS(string S, string T,int si,int num, vector<char> &tp)
{ if(num == sizeB){
answer ++;
return ;
} if(si >= sizeA || num > sizeB)
return ; for(int i = si; i<sizeA ; i++)
{
if(S[i] == T[num])
{
tp.push_back(S[i]) ;
DFS(S,T,i+, num+, tp);
tp.pop_back() ; }
}
}
int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sizeA = S.size();
sizeB = T.size(); if(sizeA < sizeB) return ; answer = ;
int i;
for( i= ; i< sizeA ; i++)
if(S[i] == T[])
break ; if(i < sizeA)
{ vector<char> tp;
DFS(S,T,i,,tp) ; } return answer;
}
private :
int answer ;
int sizeA;
int sizeB; };

上述代码使用DFS来做,大数据还过不了

DP:

将“S串中前m个字母的子串中包含多少个T串中前n个字母的子串”这样的问题记为A[m][n]。 可以得到递推式 :
if(S[m-1] == T[n-1]) A[m][n] = A[m-1][n-1] + A[m-1][n];
else A[m][n] = A[m-1][n-1];
再处理边界情况即可。简单起见,用类似打表记录式的递归实现。

class Solution {
public: int Dp(int m, int n, int *tp, const string &S,const string & T )
{ if(n == -) return ;
else if(m == -) return ; if(m < n) return ;
if( tp[m*sizeB+n] != - )
return tp[m*sizeB+n]; tp[m*sizeB+n] = S[m] == T[n] ? Dp(m-, n-,tp, S, T) + Dp(m-, n,tp,S,T) :
Dp(m-, n,tp,S,T) ; return tp[m*sizeB+n];
} int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sizeA = S.size();
sizeB = T.size();
int *tp = new int[sizeA * sizeB] ;
for(int i = ; i< sizeA * sizeB ;i++)
tp[i] = -; return Dp(sizeA-, sizeB-,tp,S, T) ; } private : int sizeA;
int sizeB;
};

上面必须先判断n == -1,在判断m == -1. 很重要。

一种写法:

class Solution {
public:
int numDistinct(string S, string T) {
// Note: The Solution object is instantiated only once and is reused by each test case.
//if(S == null || T == null) return -1;
int lens = S.size();
int lent = T.size(); if(lens == || lent == ) return ;
vector<vector<int>> m(lent+,vector<int>(lens+,));
for(int j = ; j <= S.length(); j++) m[][j] = ; for(int i = ; i <= lent; i++)
for(int j = i; j <= lens; j++)
m[i][j] = T[i-] != S[j-] ? m[i][j-] : m[i-][j-] + m[i][j-]; return m[lent][lens];
}
};

节省空间的写法:

class Solution {
public:
int numDistinct(string S, string T) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int M = T.length(); //subsequence length
int N = S.length();
if (M > N || M == || N==) {
return ;
}
vector<int> m(M, );
m[] = (T[] == S[]?:);
for (int i=; i<N; ++i) {
for(int j=min(i,M); j>=;--j) {
m[j] = m[j] + ((S[i]==T[j])?m[j-]:);
}
m[] = m[] + (S[i]==T[]?:);
}
return m[M-];
}
};

LeetCode_Distinct Subsequences的更多相关文章

  1. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  4. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  5. Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  7. [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. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. 【leetcode】Distinct Subsequences(hard)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 管理TEMP数据

    SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...

  2. 【转】 boot.img的解包与打包

    原文网址:http://blog.csdn.net/wh_19910525/article/details/8200372 Android 产品中,内核格式是Linux标准的zImage,根文件系统采 ...

  3. 2015第19周三小众app

    今天搜集下个人知道好玩的小众app: 1.开眼每日推荐5个小视频,世界那么大,一起来看看吧. 2.懒人周末周末去哪了?简洁美观 3.LOFTER 网易良心出品. 4.玩儿去 发现很多很多好玩的去处 5 ...

  4. 使IE6同样支持圆角效果

    之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...

  5. opencv 实现进度控制

    进度控制: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> ...

  6. 信息熵(Entropy)究竟是用来衡量什么的?

    信息熵(Entropy)究竟是用来衡量什么的? ——与Philip ZHANG商榷 思明 Philip ZHANG先生在反驳彭小明的时候,提出一个观点,他说:“ 就语言文 字来说,总体效率不是用民族主 ...

  7. python应用之文件属性浏览

    import time,os def showFilePROPERTIES(path): for root,dirs,files in os.walk(path,True): print('位置:' ...

  8. (转)Windows Server 2008 默认"照片库查看器" 无法打开图片, 只能用画图程序打开

    1.解决[启用Win2008照片查看器] Win2008 中放了一些图片,本来以为可以象Win7那样直接用“照片查看器”打开,可是Win2008默认竟然是用“画图”打开的,非常不方便. 再仔细一看,“ ...

  9. Server.HTMLEncode用法

    Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...

  10. Csharp 高级编程 C7.1.2(2)

    C#2.0  使用委托推断扩展委托的语法下面是示例  一个货币结构 代理的方法可以是实例的方法,也可以是静态方法,声明方法不同 实例方法可以使用委托推断,静态方法不可以用 示例代码: /* * C#2 ...