Distinct Subsequences
2015-09-19 13:36
387 查看
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,
Here is an example:
S =
Return
Solution:
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.
Solution:
class Solution { public: int numDistinct(string s, string t) { int m = s.length(); int n = t.length(); if(m < n) return 0; vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0)); for(int i = 0; i <= m; ++i) dp[i][0] = 1; for(int i = 1; i <= m; ++i) for(int j = 1; j <= n; ++j) { dp[i][j] = dp[i-1][j]; if(s[i-1] == t[j-1]) dp[i][j] += dp[i-1][j-1]; } return dp[m] ; } };
相关文章推荐
- bluetooth-蓝牙事件监听
- Android UI与资源总结(一)
- UITabView 小实例
- ubuntu下一个jboss-seam-2.2.2.Final/examples/build.xml:754: warning: 'includeantruntime' was not set
- UI样式的继承
- UITableView左右滑动删除常见问题
- 真阳率(true positive rate)、假阳率(false positive rate),AUC,ROC
- iOS UICollectionView简单使用
- iOS6新特征:UICollectionView介绍 --UICollectionView不错的文章
- [裸KMP][HDU1711][Number Sequence]
- [裸KMP][HDU1711][Number Sequence]
- iOS经典讲解之妙用UITextView
- 方便的任意处使用UI线程的工具类分享
- Looper/Handler/MessageQueue/Message/AsyncResult/Registrant/RegistrantList
- UITableView中Cell重用机制导致内容重复解决方法
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
- 字符串的“压缩”
- USACO 2.1.3 Sorting a Three-Valued Sequence
- 基础知识之_int、NSInteger、NSUInteger、NSNumber的区别和联系
- PAT-PAT (Advanced Level) Practise 1101 Quick Sort (25)【二星级】