-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet116.cpp
More file actions
44 lines (35 loc) · 1.05 KB
/
leet116.cpp
File metadata and controls
44 lines (35 loc) · 1.05 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
int dp[1001][1001];
class Solution {
public:
const int mod=1e9+7;
int freq[26][1000] = {0};
int f(int i, int j, const vector<string>& words, const string& target) {
if (j==0) return 1;
if (i<0 || i<j) return 0;
if (dp[i][j] != -1) return dp[i][j];
long long cnt = 0;
cnt+=f(i-1, j, words, target);
long long fc=freq[target[j-1]-'a'][i-1];
if (fc > 0)
cnt+= f(i-1, j-1, words, target)*fc;
return dp[i][j]=cnt%mod;
}
int numWays(vector<string>& words, const string& target) {
const int m=target.size(), n=words[0].size();
//Count freq
memset(freq, 0, sizeof(freq));
for (int i = 0; i<n; i++) {
for (const auto& w : words)
freq[w[i]-'a'][i]++;
}
memset(dp, -1, sizeof(dp));
// Call the recursive function
return f(n, m, words, target);
}
};
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();