P3808 【模板】AC 自动机(简单版)
作者:互联网
题目链接
代码
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1000010;
int n;
char str[N];
int tr[N][26],cnt[N],idx;
int fail[N],q[N];
void insert (char s[N]) {
int p = 0;
for (int i = 0;s[i];i++) {
int t = s[i] - 'a';
if (!tr[p][t]) tr[p][t] = ++idx;
p = tr[p][t];
}
cnt[p]++;
}
void get_fail () {
int hh = 0,tt = -1;
for (int i = 0;i < 26;i++) {
if (tr[0][i]) q[++tt] = tr[0][i];
}
while (hh <= tt) {
int t = q[hh++];
for (int i = 0;i < 26;i++) {
int p = tr[t][i];
if (!p) tr[t][i] = tr[fail[t]][i];
else {
fail[p] = tr[fail[t]][i];
q[++tt] = p;
}
}
}
}
int main () {
scanf ("%d",&n);
for (int i = 1;i <= n;i++) {
char x[N];
scanf ("%s",x);
insert (x);
}
get_fail ();
scanf ("%s",str);
int ans = 0;
for (int i = 0,j = 0;str[i];i++) {
int t = str[i] - 'a';
int p = j = tr[j][t];
while (p && cnt[p]) { //这里如果某一个点不是单词节点,就可以直接退掉
ans += cnt[p];
cnt[p] = 0;
p = fail[p];
}
}
printf ("%d\n",ans);
return 0;
}
标签:AC,int,tr,cnt,char,++,P3808,fail,模板 来源: https://www.cnblogs.com/incra/p/16642776.html