-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinct_Subsequences.cpp
More file actions
51 lines (43 loc) · 1.3 KB
/
Distinct_Subsequences.cpp
File metadata and controls
51 lines (43 loc) · 1.3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Source : https://oj.leetcode.com/problems/distinct-subsequences/
// Author : zheng yi xiong
// Date : 2014-12-17
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int numDistinct(string S, string T) {
vector<int> f(T.size() + 1);
f[0] = 1;
for (int i = 0; i < S.size(); ++i)
{
//f(i,j) = (T(j) == S[i]) ? f(i - 1, j) + f(i - 1, j - 1) : f(i - 1, j)
for (int j = T.size() - 1; j >= 0; --j)
{
if (T[j] == S[i])
{
f[j + 1] = f[j + 1] + f[j];
}
}
}
return f[T.size()];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
string s = "rabbbit";
string t = "rabbit";
Solution so;
int num = so.numDistinct(s, t);
return 0;
}