HDU 2222 Keywords Search (AC自动机)

题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同

AC自动机:trie树上进行KMP。首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在这一位不匹配时转移到失配指针上。失配指针是转移到某个等于此位置最长后缀的位置,求法是bfs

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=1<<28;
const double Pi=acos(-1.0);
const int Mod=1e9+7;
const int Max=1000010;
const int NumSize=26;
struct node
{
    int next[NumSize],coun;
    int fail;//fail指针
    void init()
    {
        memset(next,0,sizeof(next));
        coun=fail=0;
    }
} ACFA[Max];
int tot;
void Init()
{
    tot=0;
    ACFA[tot].init();
    return;
}
char str[Max];
void Insert(int len)//添加
{
    int now=0,mpos;
    for(int i=0; i<len; ++i)
    {
        mpos=str[i]-‘a‘;
        if(!ACFA[now].next[mpos])
        {
            ACFA[now].next[mpos]=++tot;
            ACFA[tot].init();
        }
        now=ACFA[now].next[mpos];
    }
    ACFA[now].coun++;
    return;
}
int que[Max];//bfs构造fail指针
void GetFail()//构造fail指针
{
    int top=0,bot=0;
    int now,fail;
    for(int i=0;i<NumSize;++i)//入队
        if(ACFA[0].next[i])
        que[top++]=ACFA[0].next[i];
    while(top!=bot)
    {
        now=que[bot++];
        for(int i=0;i<NumSize;++i)
        {
            if(ACFA[now].next[i])
            {
            que[top++]=ACFA[now].next[i];
            fail=ACFA[now].fail;
            while(fail&&!ACFA[fail].next[i])//寻找失配指针位置
                fail=ACFA[fail].fail;
            if(ACFA[fail].next[i])//找到
                fail=ACFA[fail].next[i];
            ACFA[ACFA[now].next[i]].fail=fail;
            }
           else//建立trie图
            ACFA[now].next[i]=ACFA[ACFA[now].fail].next[i];
        }
    }
    return;
}
int Search(int len)
{
    int ans=0;
    int mpos,nowp,now=0;
    for(int i=0;i<len;++i)
    {
        mpos=str[i]-‘a‘;
        while(now>0&&!ACFA[now].next[mpos])//失配后
            now=ACFA[now].fail;
        if(ACFA[now].next[mpos])//找到
        {
            now=ACFA[now].next[mpos];
            nowp=now;
            while(nowp&&ACFA[nowp].coun!=-1)//-1找过
            {
                ans+=ACFA[nowp].coun;
                ACFA[nowp].coun=-1;
                nowp=ACFA[nowp].fail;
            }
        }
    }
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        Init();
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
        {
            scanf("%s",str);
            Insert(strlen(str));
        }
        GetFail();
        scanf("%s",str);
        printf("%d\n",Search(strlen(str)));
    }
    return 0;
}
时间: 2024-08-08 01:29:38

HDU 2222 Keywords Search (AC自动机)的相关文章

HDU 2222 Keywords Search AC自动机入门题

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

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自动机入门 模板)

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

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

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自动机入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串.其中模式串可以重复.问有多少文本串在模式串中出现过.(对于相同的模式串次数仍然累加) 思路:ac自动机裸题: KMP是先将文本串进行匹配得到失配边f[];但是并不适用于文本串较长,模式串较多的情况.因为每次查询的时间复杂度为O(n+m).n,m分别为文本串和模式串的长度: ac自动机就是建立在Trie上,

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

原题大意:原题链接 先给定T个单词,然后给定一个字符串,查询该字符串中包含多少个给定的单词 解题思路:AC自动机模板题 参考链接:哔哩哔哩算法讲堂 WA版本 注意:因为输入的单词可能有重复,那么Insert()函数中p->id=id;语句中p->id会被覆盖,在Query()函数中会一次性全部被清零,导致不能查询重复单词,以至于结果res错误. #include<queue> #include<cstdio> #include<cstring> using

HDU 2222 Keywords Search AC自动机模板

题目链接: hdu2222 代码: #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<queue> using namespace std; struct node { int sum; node* fail; node* next[26]; node() { sum=0; fail=NULL; for(int i=0; i<26;

HDU 2222 Keyword Search AC自动机模板

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io

hdoj 2222 Keywords Search(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包含的关键字: 假设待查找的字符串为str[0..n],则str[i…j]可能为某一个关键字:假设当前正在匹配字符str[k],则以str[i..k]为关键字的所有可能 可能的关键字的最后一个字符为str[k],使用fail指针进行跳转并判断以str[k]结尾的该结点是否为关键字最后一个结点,重复进行