LeetCode_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.
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的更多相关文章
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- jquery+ajax分页
先看效果图:
- js深入研究之函数内的函数
第一种 function foo() { ; function bar() { a *= ; } bar(); return a; } 第二种 function foo() { ; function ...
- Linux企业级项目实践之网络爬虫(11)——处理http请求头
http请求头,HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.HTTP客户程序(例如浏览器),向服务 ...
- CSS flex让所有灵活的项目都带有相同的长度,忽略它们的内容:
.flexbox { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; displa ...
- Oracle Database 12c Using duplicate standby database from active database Created Active DataGuard
primary database db_name=zwc, db_unique_name=zwc standby database db_name=zwc, db_unique_name=standb ...
- pyqt一个小例子
# -*- coding: utf-8 -*- __author__ = 'Administrator' from PyQt4 import Qt,QtCore,QtGui import sys,ra ...
- css中的字体样式及元素样式
css中的字体样式一般包含有就9中,常见的有7种.这7种依次为: 1.字体样式:font-family: 2.字体大小:font-size: 3.字体加粗:font-weight: 4.字体斜体:fo ...
- <Win32_6>程序员求爱的创意程序^_^
作为程序员,我们时常被外界误认为很闷.不浪漫.没创意……等等这一类人,这让我们实在有些感伤啊,我得为程序员呐喊一声: 我们也能可以欢快.浪漫.有创意…… 朋友,你向女生表白过吗? …… 这个问题有点儿 ...
- windows 运行打开服务命令
转载自:http://www.2cto.com/os/201209/157464.html windows运行打开服务命令 Java代码 1. gpedit.msc-----组策略 2. sndr ...
- c++读取ccbi
loader类文件: 需要定义CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ButtonTestLayerLoader, loader); 这个宏定义是定义静态的l ...