hdu2222 Keywords Search & AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222

思路:AC自动机入门题,直接上AC自动机即可。

对于构建AC自动机,我们要做的只有三件事:

1)构建字典树

2)构建失败指针

3)构建trie图(这道题好像不做这一步也能A。。。但是这一步不做是会被卡成O(n^2)的。。。)

1)第一步还是比较好理解的

根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间。

加入一个模式串,我们就在trie树上一步一步走,没有这个儿子就新建,否则就按原来的边走。

2)第二步有一点复杂。

学AC自动机要有KMP的基础原因就在这里。fail指针就是KMP的next数组在有多个模式串时的升级版

KMP的next数组是指同一个模式串中,既是最长前缀又是后缀的子串的长度,这么定义是为了方便在匹配失败时跳转

而fail指针的作用也是为在匹配失败时跳转。因为是多串,一个点的fail指针指向与这一段的后缀相同的,另一个串前缀,这样就实现了在匹配失败时跳转。画个图更直观

然后就是求出fail指针了。求next数组时,我们可以由之前的next数组进行跳转得到现在的next数组,求fail指针也很类似。

我们可以一层一层地用bfs求出fail指针。

1.根节点的fail指向null,根节点的子节点的fail指向根(因为第一位就不匹配了,当然要重新开始)

2.求完fail的放到队尾

3.取出队头节点x,求它的所有儿子ch[x][i]的fail指针

如果ch[x][i]存在,那么fail[ch[x][i]]=ch[fai[x]][i](其实应该一直跳fail直到ch[x][i]存在,这也是不构建trie图会被卡成O(n^2)的原因,把不存在的儿子补好后,就只要跳一次了)

否则ch[x][i]=ch[fail[x]][i](其实就是第三步构建trie图,把所有不存在的儿子补好,省得每次跳很多次fail)

4.重复2和3直到队列为空

这样,AC自动机就构建好了

然后就是代码了。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn=1000010,maxm=250010;
using namespace std;
int cas,n;char s[maxn];

struct AC_DFA{
    int tot,ch[maxm][26],fai[maxm],sum[maxm],q[maxm],head,tail;
    void clear(){tot=0,memset(ch,0,sizeof(ch)),memset(fai,0,sizeof(fai)*2);}
    void insert(){
        int p=0;
        for (int i=1;s[i];p=ch[p][s[i]-'a'],i++) if (!ch[p][s[i]-'a']) ch[p][s[i]-'a']=++tot;
        sum[p]++;
    }
    void getfail(){
        head=0,q[tail=1]=0,fai[0]=-1;
        while (head!=tail){
            int x=q[++head];
            for (int i=0;i<26;i++)
                if (ch[x][i]){
                    q[++tail]=ch[x][i];
                    int p=x==0?0:ch[fai[x]][i];
                    //while (p!=-1&&!ch[p][i]) p=fai[p];
                    fai[ch[x][i]]=p;
                }
                else ch[x][i]=x==0?0:ch[fai[x]][i];
        }
    }
    void work(){
        int ans=0;
        for (int i=1,p=0;s[i];i++){
            while (p&&!ch[p][s[i]-'a']) p=fai[p];
            p=ch[p][s[i]-'a'];
            for (int t=p;t;t=fai[t]) ans+=sum[t],sum[t]=0;//printf("%d %d\n",t,sum[t]),
        }
        printf("%d\n",ans);
    }
}T;

int main(){
    scanf("%d",&cas);
    while (cas--){
        scanf("%d",&n);T.clear();
        for (int i=1;i<=n;i++)
            scanf("%s",s+1),T.insert();
        T.getfail(),scanf("%s",s+1),T.work();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 17:25:52

hdu2222 Keywords Search & AC自动机学习小结的相关文章

hdu2222 Keywords Search ac自动机

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 56558    Accepted Submission(s): 18493 Problem Description In the mo

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

AC自动机学习小结

AC自动机 简要说明 \(AC\) 自动机,全称 \(Aho-Corasick\ automaton\) ,是一种有限状态自动机,应用于多模式串匹配.在 \(OI\) 中通常搭配 \(dp\) 食用.因为它是状态自动机. 感性理解:在 \(Trie\) 树上加上 \(fail\) 指针.具体的讲解可以去看dalao们的博客(因为我实在是太菜了讲不好). 题目 Keywords Search 题目:给若干个模式串,再给一个文本串,问有几个模式串在文本串中出现过. 板子题.注意一个模式串只被计算一次

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

HDU 2222 Keywords Search (AC自动机模板题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

Match:Keywords Search(AC自动机模板)(HDU 2222)

多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #define MAX 26 6 7 using namespace std; 8 9 struct node 10 {