hdu2222--Keywords Search+AC自动机模板

题目链接:点击进入

KMP对模式串进行处理,然后就可以方便的判断模式串是否在目标串中出现了;这显示适合一个模式串多个目标串的情况。但是如果模式串有多个,这时如果还用KMP则需要对每个串都进行一次处理,显然不是很合适。其实这时候可以将所有模式串建成一棵trie树,然后采用类似于kmp的方法计算出failed指针,也就可以方便的进行匹配了。其实这就是ac自动机的思想。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn=500000+100;

struct Trie
{
    ///next数组表示的就是trie树
    ///fail数组记录了每个节点的失败指针
    ///end数组记录了以当前节点作为结尾的字符串的个数
    int next[maxn][26],fail[maxn],end[maxn];
    ///root表示trie的根节点,L表示当前trie的节点总数
    int root,L;
    int newnode()
    {
        for(int i=0;i<26;i++)
          next[L][i]=-1;
        end[L++]=0;
        return L-1;
    }

    void init()
    {
        L=0;
        root=newnode();
    }

    void insert(char buf[])
    {
        int len=strlen(buf);
        int now=root;
        for(int i=0;i<len;i++)
        {
            if(next[now][buf[i]-‘a‘]==-1)
              next[now][buf[i]-‘a‘]=newnode();
            now=next[now][buf[i]-‘a‘];
        }
        end[now]++;
    }

   ///采用bfs形式构造fail指针
    void build()
    {
        queue<int>Q;
        fail[root]=root;
        for(int i=0;i<26;i++)
        {
            ///如果当前节点的第i个儿子不存在
            ///则将其补上,为当前节点失败节点的第i个儿子
           if(next[root][i]==-1)
              next[root][i]=root;
           else
           {
               fail[next[root][i]]=root;
               Q.push(next[root][i]);
           }
        }
        while(!Q.empty())
        {
            int now=Q.front();
            Q.pop();
            for(int i=0;i<26;i++)
            {
                if(next[now][i]==-1)
                   next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
            }
        }
    }

    int query(char buf[])
    {
        int len=strlen(buf);
        int res=0;
        int now=root;
        for(int i=0;i<len;i++)
        {
            now=next[now][buf[i]-‘a‘];
            int temp=now;
            while(temp != root)
            {
                res+=end[temp];
                end[temp]=0;
                temp=fail[temp];
            }
        }
        return res;
    }
};

char str[maxn*2];
Trie ac;

int main()
{
   int T;
   int n;
   scanf("%d",&T);
   while(T--)
   {
       scanf("%d",&n);
       ac.init();
       for(int i=0;i<n;i++)
       {
           scanf("%s",str);
           ac.insert(str);
       }
       ac.build();
       scanf("%s",str);
       printf("%d\n",ac.query(str));
   }
  return 0;
}

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

时间: 2024-08-01 04:40:52

hdu2222--Keywords Search+AC自动机模板的相关文章

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 {

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

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

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

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

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

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

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

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