标签:
链接:点击打开链接
题意:t组数据,给出n个单词,再给一句话,问这句话中出现过几个给出的单词
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <string.h>
using namespace std;
struct node{
int str[26],fail;
short dis;
}ch[250005]; //这道题的内存卡的特别死,必须开成结构体,不能开成二维数组
int root; //而且强行改成short才不MLE
void insert(char *s){
int u=0;
for(;*s;s++){
if(!ch[u].str[*s-'a'])
ch[u].str[*s-'a']=root++;
u=ch[u].str[*s-'a'];
}
ch[u].dis++; //构造一颗字典树
}
void getfail(){ //AC自动机就是在字典树上实现KMP,因此要有一个像KMP中next数组一样的东西
int u,v,i,temp; //因此产生了fail指针,代表匹配失败后移动的方向,我认为fail数组的含义就是
queue<int>q; //找一个最长的后缀,并且这个后必须是其它串的前缀(从第一个字符开始和从查
while(!q.empty())q.pop(); //找的那个字符往后同时比较相同"个数"的字符直到不相等时叫做前缀),
q.push(0); //初始化队列
while(q.size()){
u=q.front();q.pop(); //就是bfs搜索找到每个节点的fail值
for(i=0;i<26;i++)
if(v=ch[u].str[i]){ //找哪个字符是在字典树上的
if(u==0) //根节点下所连的fail值皆为0,因为与根节点直接相连的不可能有重复的字母,因此
ch[v].fail=0; //一旦匹配失败直接移动到根节点
else{
temp=ch[u].fail; //u表示当前字符在字典树中的状态
while(temp&&!ch[temp].str[i]) //当u的fail指针不为零时,也就是说状态为u的字符有对应的前缀与之匹配时,但是
temp=ch[temp].fail; //ch[temp].str[i]为零,则意味着与状态为u的字符匹配的字符的下一位与状态为u
ch[v].fail=ch[temp].str[i]; //的字符的下一位不匹配,所以继续沿着fail指针走,然后将最终的值付给v所对应的
} //fail指针
q.push(v); //v入列继续搜索
}
}
}
int acauto(char *s){ //这个函数就是匹配的函数,也就会用到fail指针
int sum,temp,star;
sum=temp=0;
for(;*s;s++){
while(temp&&!ch[temp].str[*s-'a']) //这步在第一次循环时不会用到,与得到fail指针时相似,就是状态为temp的字符所指向的
temp=ch[temp].fail; //下一个字符并不存在,沿着fail指针移动,找到尽可能能匹配的
star=temp=ch[temp].str[*s-'a']; //将第一个字符的初始状态赋给temp和star
while(ch[star].dis){
sum+=ch[star].dis;
ch[star].dis=0; //假如有完整的字符串出现,则加上个数
star=ch[star].fail; //之后沿着fail指针移动,也就是说看已经匹配的后缀还有没有可能出现以这个后缀为
} //前缀的其它字符串,也就是fail指针的意义所在
// sum+=ch[ch[star].fail].dis; //注释掉的这两行,网上很多版本是有这两行代码的,但我认为当单词不匹配的时候一定
// ch[ch[star].fail].dis=0; //不会再出现能够匹配的单词,因为与根节点相连的一定不会出现同样的字母
}
return sum;
}
int main(){
int t,i,n;
char s[60],temp[1000005];
scanf("%d",&t);
while(t--){
memset(ch,0,sizeof(ch)); //初始化字典树
root=1;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
scanf("%s",s);
insert(s);
}
// for(i=0;i<10;i++){ //可以看看字典树是怎么连接的
// for(int j=0;j<26;j++)
// cout<<ch[i].str[j];
// cout<<endl;
// }
getfail();
scanf("%s",temp);
printf("%d\n",acauto(temp));
} //我也是初学AC自动机,看了好多版本的模板,选了一个我自己认为好理解的
return 0; //希望能够帮到别人,并且有说的不对的地方也欢迎评论指正
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/stay_accept/article/details/47613961