AC自动机

参考博客+视频

简要介绍AC自动机:

Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一。一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。

实现:KMP(思想)+Trie(字典树)+BFS搜索

例题

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 48520 Accepted Submission(s): 15473

Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output
Print how many keywords are contained in the description.

Sample Input

1
2
3
4
5
6
7
8
1
5
she
he
say
shr
her
yasherhs

Sample Output

1
3

模板

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000006;
const int MAXTOT=500005;

struct state
{
int next[26];
int fail,cnt;
}stateTable[MAXTOT];
int size=1;
queue<int> que;
void init()//初始化
{
while(!que.empty()) que.pop();//初始化队列
for(int i=0;i<MAXTOT;i++)//初始化字典树
{
memset(stateTable[i].next,0,sizeof(stateTable[i].next));
stateTable[i].fail=stateTable[i].cnt=0;
}
size=1;
}
void insert(string s)//字典树添加操作
{
int n=s.length();
int root=0;
int id;
for(int i=0;i<n;i++)
{
id=s[i]-'a';
if(stateTable[root].next[id]==0)//如果这个节点空的
{
stateTable[root].next[id]=size++;//就重新创建一个节点
}
root=stateTable[root].next[id];//更新位置
}
stateTable[root].cnt++;//表示这个节点出现过几个词,用于重复的单词
}
void getfail()//获取匹配失败后转向的指针
{
stateTable[0].fail=-1;//根节点的fail指向-1;
que.push(0);//根节点加入队列
while(!que.empty())//bfs操作,bfs操作,bfs操作,bfs操作
{
int u=que.front();//取出一个节点,由于是front所以肯定是
que.pop();
for(int i=0;i<26;i++)
{
if(stateTable[u].next[i])
{
if(u==0)
stateTable[stateTable[u].next[i]].fail=0;//如果是根节点,那么fail就指向根
else
{
int v=stateTable[u].fail;//否则就向上,找他父亲的fail节点。
while(v!=-1)//向上直到查到根节点为止
{
if(stateTable[v].next[i])//如果找到了fail节点
{
stateTable[stateTable[u].next[i]].fail=stateTable[v].next[i];//将这个节点变为u节点的fail。
break;
}
v=stateTable[v].fail;//没有找到,继续向父亲的父亲的……找。
}
if(v==-1)//最后还是没有找到
stateTable[stateTable[u].next[i]].fail=0;//fail指向根
}
que.push(stateTable[u].next[i]);//当前节点入队,继续向下搜索。
}
}
}
}
int Get(int u)
{
int res=0;
while(u)//求所有祖先的和。
{
res+=stateTable[u].cnt;
stateTable[u].cnt=0;//点算过之后就不能再算了。
u=stateTable[u].fail;
}
return res;
}
int search(string s)//开始匹配
{
int n=s.length();
int res = 0,root=0;
for(int i=0;i<n;i++)
{
int id=s[i]-'a';
if(stateTable[root].next[id]!=0)
root=stateTable[root].next[id];//基本查找操作
else//如果查找失败
{
int p=stateTable[root].fail;//获取当前节点的fail指向
while(p!=-1&&stateTable[p].next[id]==0)//如果这个fail也无法继续匹配
p=stateTable[p].fail;//继续找fail的fail节点。
if(p==-1)//如果搜到根了
root=0;//那么就重新匹配
else//否则就可以继续匹配。
root=stateTable[p].next[id];
}
if(stateTable[root].cnt)//如果这个点还有串没有被匹配
res+=Get(root);//计算以这个串为后缀的有多少个。
}
return res;
}
int main()
{
int t,n;
cin>>t;
string b;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>b;
insert(b);
}
getfail();
string s;
cin>>s;
printf("%d",search(s));
}
}
坚持原创技术分享,您的支持也将成为我的动力!
-------------本文结束感谢您的阅读-------------
undefined