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 ...
随机推荐
- 利用php unpack读取c struct的二进制数据,struct内存对齐引起的一些问题
c语言代码 #include <stdio.h> struct test{ int a; unsigned char b; int c; }; int main(){ FILE *fp; ...
- jQuery自定义函数验证邮箱格式
jQuery.fn.checkEmail = function() { // 自定义jQuery方法 var email_val = $(this).val(); reg = /^\w+([-+.]\ ...
- 【转】Android API 中文(14) —— ViewStub
用一个XML源填充view.inflate(上下文对象,资源文件Id,父窗口组一般为null): 原文网址:http://www.cnblogs.com/over140/archive/2010/10 ...
- LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下: LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径 ...
- 【POJ2242】The Circumference of the Circle(初等几何)
已知三点坐标,算圆面积. 使用初等几何知识,根据海伦公式s = sqrt(p(p - a)(p - b)(p - c)) 和 外接圆直径 d = a * b * c / (2s) 来直接计算. #in ...
- x2engine
x2engine 各版本下载 https://bitnami.com/stack/x2crm/installer https://bitnami.com/redirect/to/36211/bitna ...
- Makefile学习(二)条件判断和内嵌函数
第七章:Makefile的条件执行 条件语句可是是两个不同的变量.或者变量和常量值的比较: 7.1例子: 对变量“CC”进行判断,其值如果是“gcc ”那么在程序连接时使用库“libgnu.so”或者 ...
- 辛星解读mysql的用户管理
可能做开发的多半不太关注这方面,可是要说到做运维.那就不能不关注了.由于我们都知道,root的权限太大了.不是随便能用的.我们平时最好用一些比較低的权限的用户.这样会让我们的安全性大大提高,也能防止我 ...
- JMeter数据库性能测试
要测试一个服务器的性能,客户要求向数据库内 1000/s(每插入一千条数据)的处理能力 前提条件:一个数据库:test 数据库下面有一张表:user 表中有两个字段:username.pass ...
- IE常见的CSS的BUG(一)
2011年6月,我毕业了.2012年我接触了CSS,本以为会好过些能赚点钱了,可谁知,Internet Explorer(下称IE),这个前端工程师的噩梦浏览器让我不再那么好过了.各种出现在IE身上的 ...