leetcode_question_115 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.
Recurse:
Judge Small: Accepted!
Judge Large: Time Limit Exceeded
int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int slen = S.length();
int tlen = T.length();
if(slen <= tlen){
if(S == T) return 1;
else return 0;
}
if(S[slen-1] != T[tlen-1]) return numDistinct(S.substr(0,slen-1), T);
else
return numDistinct(S.substr(0,slen-1), T) + numDistinct(S.substr(0,slen-1), T.substr(0,tlen-1));
}
dp:
Judge Small: Accepted!
Judge Large: Accepted!
int numDistinct(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int col = S.length() + 1;
int row = T.length() + 1;
int** dp = new int*[row];
for(int i = 0; i < row; ++i)
dp[i] = new int[col];
for(int i = 0; i < row; ++i)
dp[i][0] = 0;
for(int j = 0; j < col; ++j)
dp[0][j] = 1;
for(int i = 1; i < row; ++i)
for(int j = 1; j < col; ++j)
if(T[i-1] == S[j-1]) dp[i][j] = dp[i-1][j-1] + dp[i][j-1];
else dp[i][j] = dp[i][j-1];
int tmp = dp[row-1][col-1];
for(int i = 0; i < row; ++i)
delete[] dp[i];
delete[] dp;
return tmp;
}
leetcode_question_115 Distinct Subsequences的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
随机推荐
- 论山寨手机与Android联姻 【9】SmartPhone的硬件结构
如何区别智能手机(SmartPhone)与功能手机(FeaturePhone)? 有一种观点认为,智能手机本质上是功能手机与便携式电脑(Laptop PC)的结合.功能手机的功能受限于制造厂商的预制, ...
- 学习Emacs系列教程
emacs最简单入门,只要10分钟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- 图像处理库的比较:OpenCV,FreeImage,CImg,CxImage
1.对OpenCV 的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善,操作手册很详细,手册首先给大家补计算机视觉的知识,几乎涵盖了近10年内的主流算法: 然后将图像格式和矩阵运算, ...
- POJ 1182 食物链(并查集拆点)
[题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...
- 2014上半年acm总结(1)(入门+校赛)
大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干= = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...
- C++面向对象编程初步
1,使用const 指针; const int * pOne; //指向整型常量的指针,指向的值不能修改; int * const pTwo; //指向整型的常量指针,指向的值可以修改,但该指针不能再 ...
- javascript数组去重算法-----2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vb.net常用函数
当然,这些都可以从MSDN查到,但是有时候打开帮助老慢的,所以先放到这里放一放,查个函数什么的比较快一点.都是从网上搜来的.Abs(number) 取得数值的绝对值. Asc(String) 取得字符 ...
- Eclipse 浏览文件插件- OpenExplorer
http://blog.csdn.net/w709854369/article/details/6599167 EasyExplorer 是一个类似于 Windows Explorer的Eclips ...
- SQLServer 中实现类似MySQL中的group_concat函数的功能
SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起. 后在网上查找,找到了可以实现此功能的方法,特此记录下. SELECT a, stuff((SELECT ...