首先AC自动机并不能自动写代码AC题目。。。我很失望
它解决的是快速多模式匹配的问题。
关于AC自动机的讲解:
视频:
https://www.bilibili.com/video/BV1uJ411Y7Eg?from=search&seid=17940790043002047798
文章:
https://oi-wiki.org/string/ac-automaton/
https://blog.csdn.net/bestsort/article/details/82947639
https://baijiahao.baidu.com/s?id=1610756759406088822&wfr=spider&for=pc
Keywords Search
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
5
she
he
say
shr
her
yasherhs
Sample Output
3
题目出处:HDU2222
题目大意:一共t组数据,然后是个n,然后n个word,接着一个text,问在text中有多少word(没说word不重复),就是个AC自动机的板子题
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
const int maxntext=1e6+10;
int trie[maxn][26],endtag[maxn],fail[maxn],queue[maxn],front,rear,cnt,ans;
char text[maxntext],word[55];
void buildtrie(char *w)//建立字典树
{
int len,c,u=0;
len=strlen(w);
for(int i=0;i<len;i++)
{
c=w[i]-'a';
if(trie[u][c]==0)
trie[u][c]=++cnt;
u=trie[u][c];
}
endtag[u]++;//输入数据并没有保证word是不一样的
}
void buildfail()//建立fall数组,其实于此同时也更改了字典树,变成了字典图,核心算法是bfs
{
int u=0;//根节点是个入口,并没有存放任何字符信息
for(int i=0;i<26;i++)// 先把第一层的节点入队,因为初始化的trie和fail都是0所以本身就正确,只需入队即可
if(trie[u][i])
queue[rear++]=trie[u][i];
while(front<rear)//队列不空
{
u=queue[front++];
for(int i=0;i<26;i++)//每次取出队头,更新儿子并入队
{
if(trie[u][i])
{
fail[trie[u][i]]=trie[fail[u]][i];//精髓部分,如果该儿子存在,那么更新该儿子的fail,这个更新的保证前提是下面的else操作
queue[rear++]=trie[u][i];
}
else
trie[u][i]=trie[fail[u]][i];//精髓部分,将本来应该顺着fail值跳跃的步骤进行了路径压缩,相当于直接更新指向能到c儿子的节点,也是这里将字典树变成了字典图
}
}
}
void query(char *t)
{
int len,c,u=0;
len=strlen(t);
for(int i=0;i<len;i++)
{
c=t[i]-'a';
u=trie[u][c];
for(int j=u;j&&endtag[j]!=-1;j=fail[j])//如果指向了根节点,那么退出,如果j节点已经被找到过,那么上次找到时j的fail的那些节点也被统计过了,退出
{
ans+=endtag[j];
endtag[j]=-1;//-1代表已经被找到过了
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(trie,0,sizeof(trie));
memset(fail,0,sizeof(fail));
memset(endtag,0,sizeof(endtag));
cnt=front=rear=ans=0;
int n;
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s",word);
buildtrie(word);
}
buildfail();
scanf("%s",text);
query(text);
printf("%d\n",ans);
}
return 0;
}
2021.8.4