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 ...
随机推荐
- 小qyvlik 先看两个视频,和 QtQuick UI 问答
http://edu.csdn.net/course/detail/1042 http://edu.csdn.net/course/detail/335 http://blog.csdn.net/qy ...
- C#读取文件高效方法实现
C# Code 12345678910111213141516171819202122232425262728293031 private void button1_Click ...
- (转载)tarjan求割点
割点是无向图中去掉后能把图割开的点.dfs时用dfn(u)记录u的访问时间,用low(u)数组记录u和u的子孙能追溯到的最早的节点(dfn值最小).由于无向图的dfs只有回边和树边,且以第一次dfs时 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- float position的測试案例
依据<a target=_blank href="http://blog.csdn.net/goodshot/article/details/44348525">htt ...
- XMPP通讯开发-服务器好友获取以及监听状态变化
在 XMPP通讯开发-好友获取界面设计 我们设计了放QQ的列表功能,这里我们获取我们服务器上的 数据. 这一部分知识我们可以查看smack_3_3_0/smack_3_3_0/documentat ...
- Android ActionBar详解(二):ActionBar实现Tabs标签以及下拉导航
一.添加标签 Tabs 在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重写onTabSelected.onTabUnselected ...
- C#递归算法详解
递归呢就是自己调用自己,在搜索文件夹下的文件和目录时也能用到,我这里就写一个简单的递归,代码如下: /// <summary> /// 递归算法 /// </summary> ...
- MySQL复制协议
http://hamilton.duapp.com/detail?articleId=27
- js html5 仿微信摇一摇
看微信摇一摇之后忽然想知道他是怎么写的.于是就在网上查了一些资料,有些是借鉴别人的.大家共同学习啊 先放代码 <body onload="init()"> <p& ...