-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmain.cpp
45 lines (43 loc) · 1.04 KB
/
main.cpp
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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int numDistinct(string s, string t)
{
int ns = s.length();
int nt = t.length();
vector<vector<unsigned long long>> dp(nt + 1, vector<unsigned long long>(ns + 1, 0));
dp[0][0] = 1;
for (int i = 0; i < nt + 1; i++)
{
for (int j = 1; j < ns + 1; j++)
{
dp[i][j] = dp[i][j - 1];
for (int k = 1; k < nt + 1; k++)
{
if (i == k && s[j - 1] == t[k - 1])
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]);
}
}
}
for (int i = 0; i < nt + 1; i++)
{
for (int j = 0; j < ns + 1; j++)
{
cout << dp[i][j] << " ";
}
cout << endl;
}
return dp[nt][ns];
}
};
int main()
{
string s = "babgbag";
string t = "bag";
Solution sln;
cout << sln.numDistinct(s, t) << endl;
return 0;
}