-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.cpp
78 lines (70 loc) · 1.73 KB
/
E.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
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 3e5 + 5;
int ch[MAXN][26],tot = 1,fail[MAXN],rt = 1,n;
std::string s[MAXN],t;
LL sm[MAXN];
inline void clear(){
FOR(i,1,tot) FOR(j,0,25) ch[i][j] = 0;
FOR(i,1,tot) fail[i] = sm[i] = 0;
tot = rt = 1;
}
inline void insert(int id){
int v = rt;
FOR(i,0,(int)s[id].length()-1){
int o = s[id][i]-'a';
if(!ch[v][o]) ch[v][o] = ++tot;
v = ch[v][o];
}
sm[v]++;
}
inline void build(){
std::queue<int> q;
FOR(i,0,25){
if(ch[rt][i]) fail[ch[rt][i]] = rt,q.push(ch[rt][i]);
else ch[rt][i] = rt;
}
while(!q.empty()){
int v = q.front();q.pop();
FOR(i,0,25){
if(ch[v][i]) fail[ch[v][i]] = ch[fail[v]][i],q.push(ch[v][i]),sm[ch[v][i]] += sm[fail[ch[v][i]]];
else ch[v][i] = ch[fail[v]][i];
}
}
// FOR(i,1,tot) sm[i] += sm[fail[i]];
}
LL pre[MAXN],suf[MAXN];
int main(){
std::ios::sync_with_stdio(false);
std::cin >> t >> n;
FOR(i,1,n) std::cin >> s[i];
FOR(i,1,n) insert(i);build();
int v = 1;
FOR(i,0,(int)t.size()-1){
int o = t[i]-'a';v = ch[v][o];
pre[i+1] = sm[v];
}
clear();
FOR(i,1,n) std::reverse(all(s[i])),insert(i);build();
v = 1;
ROF(i,(int)t.size()-1,0){
int o = t[i]-'a';v = ch[v][o];
suf[i+1] = sm[v];
}
LL ans = 0;
FOR(i,1,(int)t.size()-1) ans += 1ll*pre[i]*suf[i+1];
printf("%lld\n",ans);
return 0;
}