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 ...
随机推荐
- logstorm
http://blog.itpub.net/15480802/viewspace-688859/ http://www.csdn.net/article/2014-09-04/2821558
- iOS 倒计时
// // RootViewController.m // MyTimerDemo // // Created by huluo on 1/21/14. // Copyright (c) 2014 b ...
- hdu 1078(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 //dp[i][j]表示从点i,j处开始能获得的最多cheese #include <io ...
- Item with the same id "98" already exist
在magento项目中多次遇到这样一个错误: Item (Bluecom_Onefieldusername_Model_Customer) with the same id "98" ...
- Magento - get Attribute Options of the dropdown type attribute
$attribute_code = "color"; $attribute_details = Mage::getSingleton("eav/config" ...
- XCode工程中ARC模式与非ARC模式共用(转)
Xcode 项目中经常会融合一些老的代码,它们可能采用非ARC的模式.混合编译时,就会碰到编译出错的情况. 如何共用ARC模式和非ARC模式呢? XCode除了提供整个项目是否使用ARC模式的选择外, ...
- Genealogical tree(拓扑结构+邻接表+优先队列)
Genealogical tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- List的方法和属性 方法或属性 作用
List的方法和属性 方法或属性 作用 Capacity 用于获取或设置List可容纳元素的数量.当数量超过容量时,这个值会自动增长.您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以 ...
- eclipse建立cocos2d-x开发环境
前提: 已经安装了eclipse.能够正常开发 android应用 环境:windows 工具:1.已经集成了adt的eclipse,能够开发android应用.没有的,能够下载.下载地址:http: ...
- 【邻接表字符串Hash】【HDU1800】Flying to the Mars
题意: 给你N个数字,带前导0,问出现最多的数字个数 思路: 读入,清楚前导0,Hash. 用邻接表字符串Hash有一下几点注意 string,不要memset,否则地址也没了,涉及到stl的东西,少 ...