AC 自动机
讲解部分没啥质量,去 oi-wiki 学完直接来做题吧!
AC 自动机基于 trie 树的结构,结合 KMP 的思想实现多模式串匹配。
建 AC 自动机的大致过程为:
- 对所有模式串构建 trie 树。对于一个节点,称其「状态」为根到该节点的边组成的字符串(为某个模式串的前缀)。
- 在树上每个节点构建失配指针 fail 指向状态为当前节点的状态的最长后缀的节点。
建 trie 树
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int c = s[i] - 'a';
if (!tr[u].son[c]) tr[u].son[c] = ++ tot;
u = tr[u].son[c];
}
tr[u].cnt ++;
}
构建失配指针(fail)
定义
按照深度由小到大的顺序求 fail,即 bfs。
考虑
- 若
\operatorname{trie}(\operatorname{fail}(fa),c) 存在,则接上c ,\operatorname{fail}(u) = \operatorname{trie}(\operatorname{fail}(fa),c) ; - 否则,向上跳
fa = \operatorname{fail}(fa) ,重复上面的判断。若最终跳到根,则\operatorname{fail}(u) 为根。
void build(string s) {
queue<int> q;
for (int i = 0; i < 26; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i];
q.push(tr[u].son[i]);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
-
在 bfs 前,我们将根结点的所有儿子入队,而不是将根入队。否则会使儿子的 fail 指针指向本身
-
按照上面的说法,我们在修改儿子 fail 指针前应该不断跳 fail,但是这份代码并没有,而是在
else语句改变了 trie 的形态,用了一种类似压缩的思想,令\operatorname{trie}(u,c) = \operatorname{trie}(\operatorname{fail}(u),c) ,相当于用son直接记录下一个能匹配的位置,保证了时间复杂度。(详见 OI-Wiki)
查询
从文本串
int query(string s) {
int u = 0, ans = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[s[i] - 'a'];
int t = u;
while (t && tr[t].cnt != -1) {
ans += tr[t].cnt;
tr[t].cnt = -1;
t = tr[t].fail;
}
}
return ans;
}
注意这里的 u = tr[u].son[s[i] - 'a']:当某个节点不存在时,由于在建 fail 时将空儿子补成了 tr[u].son[c] = tr[tr[u].fail].son[c],访问空儿子相当于一路跳 fail 直到第一个有该字符儿子的状态。因此是正确的。
这份代码实现了:有多少个模式串在文本串中出现,不会重复统计一个模式串的多次出现。因此每次统计答案后将 cnt 置为零不再重复统计。
如果希望统计一个模式串的多次出现,则不需要将 cnt 置为零。
优化
如果只保留 fail 指针的连边
需要查询的信息本质上是子树内点权和。离线处理,令
这里用的是拓扑排序,以P5357 【模板】AC 自动机的代码为例:
void query(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[s[i] - 'a'];
tr[u].ans ++;
}
}
void topo() {
queue<int> q;
for (int i = 0; i <= tot; i ++) if (!tr[i].d) q.push(i);
while (!q.empty()) {
int u = q.front(); q.pop();
ans[tr[u].idx] = tr[u].ans;
int v = tr[u].fail;
tr[v].ans += tr[u].ans;
tr[v].d --;
if (!tr[v].d) q.push(v);
}
}
::::success[Code:P5357 【模板】AC 自动机]
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 2e5 + 5;
struct Node {
int son[26];
int fail, d, idx, ans;
} tr[N];
int idx[N], tot, pidx;
int ans[N];
void insert(string s, int &idx) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int c = s[i] - 'a';
if (!tr[u].son[c]) tr[u].son[c] = ++ tot;
u = tr[u].son[c];
}
if (!tr[u].idx) tr[u].idx = ++ pidx;
idx = tr[u].idx;
}
void build() {
queue<int> q;
for (int i = 0; i < 26; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i];
tr[tr[tr[u].son[i]].fail].d ++;
q.push(tr[u].son[i]);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
void query(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[s[i] - 'a'];
tr[u].ans ++;
}
}
void topo() {
queue<int> q;
for (int i = 0; i <= tot; i ++) if (!tr[i].d) q.push(i);
while (!q.empty()) {
int u = q.front(); q.pop();
ans[tr[u].idx] = tr[u].ans;
int v = tr[u].fail;
tr[v].ans += tr[u].ans;
tr[v].d --;
if (!tr[v].d) q.push(v);
}
}
signed main() {
// freopen("", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
insert(s, idx[i]);
}
build();
string s; cin >> s;
query(s);
topo();
for (int i = 1; i <= n; i ++) cout << ans[idx[i]] << endl;
return 0;
}
::::
时间复杂度
例题
多模匹配
P3966
::::info[sol] 模板题。文本串为所有模式串拼起来,其中相邻模式串用特殊字符隔开。 ::::
::::info[Code]
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <random>
#include <ctime>
#include <queue>
//#define INF 0x3f3f3f3f
//#define int long long
//#define lson (id << 1)
//#define rson (id << 1 | 1)
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 205, L = 1e6 + 5;
struct Trie {
int son[26];
int fail, d, ans;
} tr[L];
int pointcnt;
int idx[N];
void insert(string s, int &idx) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int now = s[i] - 'a';
if (!tr[u].son[now]) {
tr[u].son[now] = ++ pointcnt;
}
u = tr[u].son[now];
}
idx = u;
}
void build() {
queue<int> q;
for (int i = 0; i < 26; i ++) {
if (tr[0].son[i]) {
q.push(tr[0].son[i]);
}
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i];
tr[tr[tr[u].fail].son[i]].d ++;
q.push(tr[u].son[i]);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
void query(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = s[i] == '#' ? 0 : tr[u].son[s[i] - 'a'];
// printf("u = %d\n", u);
tr[u].ans ++;
}
}
void topo() {
// printf("topo\n");
queue<int> q;
for (int i = 1; i <= pointcnt; i ++) {
if (!tr[i].d) q.push(i);
}
while (!q.empty()) {
int u = q.front(); q.pop();
int v = tr[u].fail;
// printf("u = %d, v = %d\n", u, v);
tr[v].ans += tr[u].ans;
if (!(-- tr[v].d)) q.push(v);
}
}
signed main() {
// freopen("input.txt", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
string s, t;
for (int i = 1; i <= n; i ++) {
cin >> s;
insert(s, idx[i]);
s += '#', t += s;
}
build();
query(t);
topo();
for (int i = 1; i <= n; i ++) cout << tr[idx[i]].ans << endl;
return 0;
}
::::
P9196
::::info[sol]
将原串
| 考虑将 |
|---|
::::info[Code]
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 5e6 + 5, V = 6;
struct Node {
int son[V];
int idx, d, fail, cnt;
} tr[N];
int tot, pidx;
int idx[N], ans[N];
int get(char c) {
if (c == 'A') return 0;
if (c == 'G') return 1;
if (c == 'U') return 2;
if (c == 'C') return 3;
if (c == '#') return 4;
return 5;
}
void insert(string s, int &idx) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int id = get(s[i]);
if (!tr[u].son[id]) tr[u].son[id] = ++ tot;
u = tr[u].son[id];
}
if (!tr[u].idx) tr[u].idx = ++ pidx;
idx = tr[u].idx;
}
void build() {
queue<int> q;
for (int i = 0; i < V; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < V; i ++) {
if (tr[u].son[i]) {
int v = tr[u].son[i];
tr[v].fail = tr[tr[u].fail].son[i];
tr[tr[v].fail].d ++;
q.push(v);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
void query(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[get(s[i])];
tr[u].cnt ++;
}
}
void topo() {
queue<int> q;
for (int i = 1; i <= tot; i ++) {
if (!tr[i].d) q.push(i);
}
while (!q.empty()) {
int u = q.front(); q.pop();
ans[tr[u].idx] = tr[u].cnt;
int v = tr[u].fail;
tr[v].cnt += tr[u].cnt;
tr[v].d --;
if (!tr[v].d) q.push(v);
}
}
signed main() {
// freopen("", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
int n, m; cin >> n >> m;
string t;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
t += s + '#' + s + '*';
}
for (int i = 1; i <= m; i ++) {
string s1, s2; cin >> s1 >> s2;
insert(s2 + '#' + s1, idx[i]);
}
build();
query(t);
topo();
for (int i = 1; i <= m; i ++) cout << ans[idx[i]] << '\n';
return 0;
}
::::
P14363
::::info[sol]
按照题解的说法转换为多模匹配问题。发现我们只需要查询每个模式串的出现次数之和,于是可以在建自动机时,维护一个变量 cnt,表示从当前点不断跳 fail 直到根得到的贡献之和。这样我们在查询时,只需要求所有前缀的 cnt 和即可。
| 时间复杂度 |
|---|
::::info[Code]
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 5e6 + 5;
struct Node {
int son[27];
int cnt, fail;
} tr[N];
int tot;
int get(char c) {
if (c >= 'a' && c <= 'z') return c - 'a';
return 26;
}
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int c = get(s[i]);
if (!tr[u].son[c]) tr[u].son[c] = ++ tot;
u = tr[u].son[c];
}
tr[u].cnt ++;
}
void build() {
queue<int> q;
for (int i = 0; i < 27; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 27; i ++) {
if (tr[u].son[i]) {
tr[tr[u].son[i]].fail = tr[tr[u].fail].son[i];
tr[tr[u].son[i]].cnt += tr[tr[tr[u].son[i]].fail].cnt;
q.push(tr[u].son[i]);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
int query(string s) {
int u = 0, res = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[get(s[i])];
res += tr[u].cnt;
}
return res;
}
signed main() {
// freopen("replace4.in", "r", stdin);
// freopen("replace.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i ++) {
string s1, s2; cin >> s1 >> s2;
string L, R;
int fst = -1, lst = -1;
for (int i = 0; i < (int)s1.size(); i ++) {
if (s1[i] != s2[i]) {
fst = i; break;
}
}
for (int i = (int)s1.size() - 1; i >= 0; i --) {
if (s1[i] != s2[i]) {
lst = i; break;
}
}
string t1, t2;
for (int i = 0; i < (int)s1.size(); i ++) {
if (i < fst) L += s1[i];
else if (i <= lst) t1 += s1[i], t2 += s2[i];
else R += s1[i];
}
insert(L + '#' + t1 + t2 + '#' + R);
}
build();
while (q --) {
string s1, s2; cin >> s1 >> s2;
if (s1.size() != s2.size()) {
cout << 0 << endl; continue;
}
string L, R;
int fst = -1, lst = -1;
for (int i = 0; i < (int)s1.size(); i ++) {
if (s1[i] != s2[i]) {
fst = i; break;
}
}
for (int i = (int)s1.size() - 1; i >= 0; i --) {
if (s1[i] != s2[i]) {
lst = i; break;
}
}
string t1, t2;
for (int i = 0; i < (int)s1.size(); i ++) {
if (i < fst) L += s1[i];
else if (i <= lst) t1 += s1[i], t2 += s2[i];
else R += s1[i];
}
cout << query(L + '#' + t1 + t2 + '#' + R) << endl;
}
return 0;
}
::::
P3121
::::info[sol]
考虑将
::::info[Code]
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1e5 + 5;
struct Node {
int son[26];
int fail;
int len;
} tr[N];
int tot;
int st[N], top;
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int c = s[i] - 'a';
if (!tr[u].son[c]) tr[u].son[c] = ++ tot;
u = tr[u].son[c];
}
tr[u].len = s.size();
}
void build() {
queue<int> q;
for (int i = 0; i < 26; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
int v = tr[u].son[i];
tr[v].fail = tr[tr[u].fail].son[i];
if (!tr[v].len) tr[v].len = tr[tr[v].fail].len;
q.push(tr[u].son[i]);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
int now[N];
void query(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
u = tr[u].son[s[i] - 'a'];
now[i] = u;
st[ ++ top] = i;
int len = tr[u].len;
if (len) {
top -= len;
u = !top ? 0 : now[st[top]];
}
}
}
signed main() {
// freopen("", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
string t; cin >> t;
int n; cin >> n;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
insert(s);
}
build();
query(t);
for (int i = 1; i <= top; i ++) cout << t[st[i]];
return 0;
}
::::
AC 自动机上 dp
P2292
::::info[sol]
对于文本串
设这段前缀在 trie 上的节点为
- 设
dep_u 为u 的状态的长度,令j = dep_u - dep_{fa} 。 - 若
f_{i-j} = 1 ,则f_i = 1 。
注意到
考虑优化。状压
考虑求
单次查询时间复杂度优化为
::::info[Code]
注意 build 中初始入队的节点的
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <random>
#include <ctime>
#include <queue>
//#define INF 0x3f3f3f3f
//#define int long long
//#define lson (id << 1)
//#define rson (id << 1 | 1)
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 405;
struct trie {
int son[26];
int fail, dep, g;
bool ed;
} tr[N];
int pointcnt;
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int now = s[i] - 'a';
if (!tr[u].son[now]) {
tr[u].son[now] = ++ pointcnt;
tr[pointcnt].dep = i;
}
u = tr[u].son[now];
}
tr[u].ed = 1;
}
void build() {
queue<int> q;
int u = 0;
for (int i = 0; i < 26; i ++) {
int v = tr[u].son[i];
if (v) {
q.push(v);
tr[v].g = tr[v].ed;
}
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
int v = tr[u].son[i];
tr[v].fail = tr[tr[u].fail].son[i];
int fail = tr[v].fail;
tr[v].g = tr[fail].g + tr[v].ed * (1 << tr[v].dep);
q.push(v);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
int query(string s) {
int n = s.size(); s = ' ' + s;
int u = 0;
int S = 1, mask = (1 << 21) - 1, ans = 0;
for (int i = 1; i <= n; i ++) {
u = tr[u].son[s[i] - 'a'];
bool now = S & tr[u].g;
if (now) ans = i;
S <<= 1, S |= now;
S &= mask;
}
return ans;
}
signed main() {
// freopen("data.txt", "r", stdin);
// freopen("input.txt", "r", stdin);
// freopen("1.txt", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
int n, T; cin >> n >> T;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
insert(s);
}
build();
while (T --) {
string t; cin >> t;
cout << query(t) << endl;
}
return 0;
}
::::
P4052
::::info[sol]
考虑用
若到节点
| 设 |
|---|
::::info[Code]
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 4e5, M = 105, Mod = 1e4 + 7;
struct Node {
int son[26];
int fail; bool bad;
} tr[N];
int tot;
int f[M][N];
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int c = s[i] - 'A';
if (!tr[u].son[c]) tr[u].son[c] = ++ tot;
u = tr[u].son[c];
}
tr[u].bad = 1;
}
void build() {
queue<int> q;
for (int i = 0; i < 26; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
if (tr[u].son[i]) {
int v = tr[u].son[i];
tr[v].fail = tr[tr[u].fail].son[i];
tr[v].bad |= tr[tr[v].fail].bad;
q.push(v);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
signed main() {
// freopen("data.txt", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
insert(s);
}
build();
f[0][0] = 1;
int ans = 1; for (int i = 1; i <= m; i ++) ans = ans * 26 % Mod;
for (int i = 1; i <= m; i ++) {
for (int j = 0; j <= tot; j ++) {
for (int k = 0; k < 26; k ++) {
int v = tr[j].son[k];
if (!tr[v].bad) {
f[i][v] = (f[i][v] + f[i - 1][j]) % Mod;
}
}
}
}
for (int j = 0; j <= tot; j ++) ans = (ans - f[m][j] + Mod) % Mod;
cout << ans;
return 0;
}
::::
P3041
::::info[sol]
考虑加入一个字符,增加的贡献是新字符串的组合技后缀数量。因此预处理出每个节点状态的组合技后缀数量
::::info[Code]
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <random>
#include <ctime>
#include <queue>
//#define INF 0x3f3f3f3f
//#define int long long
//#define lson (id << 1)
//#define rson (id << 1 | 1)
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 305, K = 1e3 + 5;
struct trie {
int son[3];
int fail, val;
} tr[N];
int pointcnt;
void insert(string s) {
int u = 0;
for (int i = 0; i < (int)s.size(); i ++) {
int now = s[i] - 'A';
if (!tr[u].son[now]) {
tr[u].son[now] = ++ pointcnt;
}
u = tr[u].son[now];
}
tr[u].val ++;
}
void build() {
queue<int> q;
for (int i = 0; i < 3; i ++) {
if (tr[0].son[i]) q.push(tr[0].son[i]);
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 3; i ++) {
if (tr[u].son[i]) {
int v = tr[u].son[i];
tr[v].fail = tr[tr[u].fail].son[i];
tr[v].val += tr[tr[v].fail].val;
q.push(v);
} else {
tr[u].son[i] = tr[tr[u].fail].son[i];
}
}
}
}
int f[K][N];
signed main() {
// freopen("input.txt", "r", stdin);
// freopen("", "w", stdout);
// ios::sync_with_stdio(0); cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i ++) {
string s; cin >> s;
insert(s);
}
build();
for (int i = 0; i <= k; i ++) {
for (int j = 0; j <= pointcnt; j ++) {
f[i][j] = -1e9;
}
}
f[0][0] = 0;
for (int i = 0; i < k; i ++) {
for (int u = 0; u <= pointcnt; u ++) {
for (int x = 0; x < 3; x ++) {
int v = tr[u].son[x];
if (v) f[i + 1][v] = max(f[i + 1][v], f[i][u] + tr[v].val);
}
}
}
int ans = 0;
for (int u = 1; u <= pointcnt; u ++) ans = max(ans, f[k][u]);
cout << ans;
return 0;
}
::::
补充
P2414
::::info[]{open}
给定字符串 B、P。
维护字符串
- 小写字母:
t 的末尾加上这个字母。 B:删除t 的末尾字符。P:打印t 。
| (没写完/kk) |
|---|
::::info[Code]
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <random>
#include <map>
#include <ctime>
#include <iomanip>
// #define int long long
// #define lson (id << 1)
// #define rson (id << 1 | 1)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6 + 5;
struct Trie {
int son[27], fail, lsted, dep;
} tr[N]; int cnt = 1;
void insert(string s) {
int now = 1;
// string t;
for (int i = 0; i < (int)s.size(); i ++) {
int& v = tr[now].son[s[i] - 'a'];
// t += s[i];
if (!v) {
v = ++ cnt;
tr[v].dep = tr[now].dep + 1;
// cout << cnt << ' ' << t << endl;
}
now = v;
}
tr[now].lsted = now;
}
struct Edge {
int to, nx;
} e[N]; int hd[N], ecnt, fa[N];
void add(int u, int v) {
// printf("add(%d, %d)\n", u, v);
fa[v] = u;
e[ ++ ecnt] = {v, hd[u]}, hd[u] = ecnt;
}
void build() {
queue<int> q;
for (int i = 0; i < 26; i ++) {
int& v = tr[1].son[i];
if (v) {
tr[v].fail = 1;
add(tr[v].fail, v);
q.push(v);
} else {
v = 1;
}
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; i ++) {
int& v = tr[u].son[i];
if (v) {
tr[v].fail = tr[tr[u].fail].son[i];
add(tr[v].fail, v);
q.push(v);
} else {
v = tr[tr[u].fail].son[i];
}
}
}
}
int dfn[N], times, siz[N];
void dfs(int u) {
dfn[u] = ++ times, siz[u] = 1;
// printf("u = %d, dfn = %d\n", u, times);
for (int i = hd[u]; i; i = e[i].nx) {
int v = e[i].to;
if (tr[v].lsted != v) tr[v].lsted = tr[u].lsted;
dfs(v);
siz[u] += siz[v];
}
}
int T[N];
void update(int x, int v) {
for (; x <= times; x += (x & (-x))) T[x] += v;
}
int query(int x) {
int res = 0;
for (; x; x -= (x & (-x))) res += T[x];
return res;
}
int get(int l, int r) {
// printf("get(%d, %d) = %d - %d\n", l, r, query(r), query(l - 1));
return query(r) - query(l - 1);
}
int f[N], g[N];
int ed[N];
bool vis[N], fst[N];
int del[N], top;
string s[N];
const bool TEST = false;
signed main() {
clock_t start = clock();
// freopen("", "r", stdin);
// freopen("", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> s[i];
insert(s[i]);
}
build();
dfs(1);
int ans = 0;
for (int i = 1; i <= n; i ++) {
// printf("i = %d\n", i);
int len = s[i].size();
for (int j = 0, now = 1; j < len; j ++) {
now = tr[now].son[s[i][j] - 'a'];
update(dfn[now], 1); del[ ++ top] = dfn[now];
if (tr[now].dep == len) now = fa[now];
// printf("i = %d, j = %d, dep = %d\n", i, j, tr[now].dep);
ed[j] = tr[now].lsted;
if (!vis[ed[j]]) {
vis[ed[j]] = 1; fst[j] = 1;
// printf("j = %d, ed = %d, fst = 1\n", j, ed[j]);
}
}
for (int j = 0; j < len; j ++) {
if (!ed[j] || !fst[j]) continue;
f[ed[j]] = get(dfn[ed[j]], dfn[ed[j]] + siz[ed[j]] - 1);
// printf("j = %d, f = %d\n", j, f[j]);
}
int mnL = len + 1;
for (int j = len - 1; j >= 0; j --) {
if (!ed[j]) continue;
int t = tr[ed[j]].dep;
if ((j + 1) - t + 1 < mnL) {
g[ed[j]] ++; mnL = (j + 1) - t + 1;
// printf("g[%d] ++\n", j);
}
}
for (int j = 0; j < len; j ++) {
if (ed[j] && fst[j] && f[ed[j]] && f[ed[j]] == g[ed[j]]) ans ++;
}
for (int j = 0; j < len; j ++) {
f[ed[j]] = g[ed[j]] = 0;
vis[ed[j]] = fst[j] = 0;
ed[j] = 0;
}
while (top) update(del[top --], -1);
}
cout << ans << endl;
if (TEST) cerr << "time used: " << (double)(clock() - start) / 1000 << "ms\n";
return 0;
}
::::