-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord Combinations.cpp
More file actions
executable file
·61 lines (56 loc) · 977 Bytes
/
Copy pathWord Combinations.cpp
File metadata and controls
executable file
·61 lines (56 loc) · 977 Bytes
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
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
class Trie {
public:
Trie() {
for (int i = 0; i < 26; i++) v[i] = NULL;
end = false;
}
~Trie() {
for (int i = 0; i < 26; i++) {
if (v[i] != NULL) delete v[i];
}
}
void insert(string &w) {
Trie *t = this;
for (auto c : w) {
if (t->v[c - 'a'] == NULL) {
t->v[c - 'a'] = new Trie;
}
t = t->v[c - 'a'];
}
t->end = true;
}
Trie *v[26];
bool end;
} trie;
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
int k;
cin >> k;
for (int i = 0; i < k; i++) {
string ss;
cin >> ss;
trie.insert(ss);
}
vector<LL> dp(s.size() + 1);
dp[0] = 1;
for (unsigned i = 1; i < dp.size(); i++) {
Trie *t = ≜
unsigned j = i - 1;
while (t != NULL) {
if (t->end) {
dp[j] = (dp[j] + dp[i - 1]) % MOD;
}
if (j == s.size()) break;
t = t->v[s[j] - 'a'];
j++;
}
}
cout << dp[s.size()] << "\n";
return 0;
}