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 ...
随机推荐
- BZOJ2697: 特技飞行
2697: 特技飞行 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 607 Solved: 363[Submit][Status] Descript ...
- 同行blog收集
姜楠:http://www.slyar.com/blog/ 赵劼:http://blog.zhaojie.me/ 研究者July:http://my.csdn.net/v_JULY_v 王卓群:htt ...
- Android AutoCompleteTextView和MultiAutoCompleteTextView使用
Android AutoCompleteTextView和MultiAutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息: 它 ...
- 黑马程序员_Java面向对象_异常
6.异常 1.异常: 就是程序在运行时出现不正常的情况.问题也是现实生活中一个具体的事物,也可以通过Java的类进行描述,并封装成对象.Exception和Error的子类名都是以父类名作为后缀名. ...
- hdu2769:枚举+同余方程
题意:有一个随机数生成器 x[i+1]=(a*x[i]+b)%10001 已知 x1,x3,x5...求 x2,x4,x6...... x的个数为 2n (n<=10000) a,b也在 0 ...
- hibernate 对 sql server 2005 分页改进
Hibernate 可以实现分页查询 如下 Query q = session.createQuery("from Cat as c"); q.setFirstResult(100 ...
- 【转】打包AAC码流到FLV文件
AAC编码后数据打包到FLV很简单.1. FLV音频Tag格式 字节位置 意义0x08, ...
- windows 杀进程
selenium自动化时,会启动chromedriver.exe,每次运行一次,就多启动一个,执行多次就会拖慢系统.如下批处理命令,可以批量杀掉进程 tasklist |find "chro ...
- Java中普通代码块,构造代码块,静态代码块的代码演示样例及区分
//运行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 当中静态代码块仅仅运行一次.构造代码块在每次创建对象是都会运行. 1 普通代码块 <span ...
- Operation System - Peterson's Solution算法 解决多线程冲突
Person's solution 是用来一种基于软件的解决关键区域问题的算法(critical-section). 它并不是完美的,有可能不对地工作.并且是限制解决两个进程同步的问题. 可是它非常e ...