-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1731 - Word Combinations.cpp
71 lines (62 loc) · 1.38 KB
/
1731 - Word Combinations.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct T {
struct node {
int next[26];
bool terminal;
node() {
for (int i = 0; i < 26; i++) next[i] = -1;
terminal = false;
}
};
vector<node> st;
T() { st.emplace_back(); }
void insert(string &s) {
int n = s.length();
int cur = 0;
for (int i = 0; i < n; i++)
if (st[cur].next[s[i] - 'a'] == -1) {
st[cur].next[s[i] - 'a'] = st.size();
cur = st.size();
st.emplace_back();
} else
cur = st[cur].next[s[i] - 'a'];
st[cur].terminal = true;
}
int search(string &s) {
int n = s.length();
vector<int> dp(n + 1);
dp[0] = 1;
for (int len = 1; len <= n; len++) {
int cur = 0;
for (int i = n - len; i < n; i++) {
if (st[cur].next[s[i] - 'a'] == -1) break;
cur = st[cur].next[s[i] - 'a'];
if (st[cur].terminal) dp[len] = (dp[len] + dp[n - (i + 1)]) % mod;
}
}
return dp[n];
}
};
void solve() {
string s;
cin >> s;
int k;
cin >> k;
T t;
while (k--) {
string x;
cin >> x;
t.insert(x);
}
cout << t.search(s) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}