-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoj3181.cpp
More file actions
103 lines (93 loc) · 1.67 KB
/
zoj3181.cpp
File metadata and controls
103 lines (93 loc) · 1.67 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdio>
#include <cstring>
#include <cstdlib>
const int MAX_A = 27;
const int MAX_N = 207;
const int MAX_L = 1007;
const int MOD = (int)1E9 + 7;
char str[MAX_L], temp[MAX_N];
long long dp[MAX_L];
struct node {
node *next[MAX_A];
int val;
};
node *root;
node *addnode() {
node *p = (node *) malloc(sizeof(node));
p->val = 0;
for (int i = 0; i < MAX_A; ++i)
p->next[i] = NULL;
return p;
}
void del(node *p) {
for (int i = 0; i < MAX_A; ++i)
if (p->next[i] != NULL)
del(p->next[i]);
free(p);
}
void insert(char *s) {
node *p = root;
int i = 0;
while (s[i]) {
int q = s[i++] - 'A';
if (p->next[q] == NULL)
p->next[q] = addnode();
p = p->next[q];
}
++p->val;
}
void query() {
node *p = root;
int i = 0;
while (str[i]) {
int q = str[i] - 'A';
if (p->next[q] == NULL)
break;
p = p->next[q];
dp[i] = p->val;
++i;
}
}
long long tp[MAX_L];
void update(char *s, int idx) {
memset(tp, 0, sizeof tp);
node *p = root;
int i = 0;
long long tot = 0;
while (s[i]) {
int q = s[i] - 'A';
if (p->next[q] == NULL)
break;
p = p->next[q];
if (p->val > 0)
tp[i + idx] = (tot * p->val) % MOD;
tot = (tot + dp[i + idx]) % MOD;
++i;
}
for (int j = idx; j <= idx + i; ++j)
dp[j] = (tp[j] + dp[j]) % MOD;
}
int main() {
int T;
scanf("%d", &T);
while (T-- > 0) {
scanf("%s", str);
int n;
scanf("%d", &n);
root = addnode();
for (int i = 1; i <= n; ++i) {
scanf("%s", temp);
if (strlen(temp) == 1)
continue;
insert(temp);
}
memset(dp, 0, sizeof dp);
query();
for (int i = 1; str[i]; ++i)
update(str + i, i);
long long ans = dp[strlen(str) - 1];
printf("%lld\n", ans);
del(root);
}
return 0;
}