-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1731.cpp
46 lines (43 loc) · 860 Bytes
/
1731.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
#include <bits/stdc++.h>
using namespace std;
const int N = 5005, MOD = 1e9 + 7;
struct Node {
bool mark;
Node* nxt[26];
};
int n, k, f[N];
string s;
Node* root;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> s;
n = s.length();
root = new Node();
cin >> k;
while (k--) {
string t; cin >> t;
reverse(t.begin(), t.end());
Node* cur = root;
for (char c: t) {
int cc = c - 'a';
if (!cur->nxt[cc]) {
cur->nxt[cc] = new Node();
}
cur = cur->nxt[cc];
}
cur->mark = true;
}
f[0] = 1;
for (int i = 1; i <= n; ++i) {
Node* cur = root;
for (int j = i - 1; j >= 0; --j) {
if (!cur->nxt[s[j] - 'a']) break;
cur = cur->nxt[s[j] - 'a'];
if (cur->mark) {
(f[i] += f[j]) %= MOD;
}
}
}
cout << f[n] << "\n";
return 0;
}