hdu2222

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define N 26
#define maxn 500005
struct Trie{
    int next[500010][26],fail[500010],end[500010];
    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]++;
    }
    void build()
    {
        queue<int> Q;
        fail[root]=root;
        for(int i=0;i<26;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 now=root;
        int res=0;
        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;
    }
};
Trie ac;
int T,sz,ans,n;
char buf[2*maxn];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        sz=1,ans=0;
        scanf("%d",&n);
        ac.init();
        while(n--)
        {
            scanf("%s",buf);
            ac.insert(buf);
         }
         ac.build();
         scanf("%s",buf);
         printf("%d\n",ac.query(buf));
    }
    return 0;
 } 
时间: 2024-10-21 06:35:59

hdu2222的相关文章

// hdu2222 // AC自动机初学

// hdu2222 // #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; char k[55],s[1000005]; struct node { int fail; int nex

【HDU2222】Keywords Search(AC自动机)

Problem Description 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

hdu2222 AC自动机-给定串中出现了几个模式串

http://acm.hdu.edu.cn/showproblem.php?pid=2222 Problem Description 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 lo

【题解】HDU-2222 Keywords Search

题目链接:HDU-2222  或  Vjudge 简单说明: ac自动机的建立,其中插入过程借助了字典树,处理回溯数组(也有人称失败数组)过程是一个广搜运用了STL的队列(queue).ac自动机的过程还在写. 这是学习ac自动机的第一题,如果wa的话,那就要注意,字串结束标记是如何标记的了.它不能单单 记录,还要 计数.WA了好几次-- my codes: #include <algorithm> #include <iostream> #include <cstring&

ac自动机基础模板(hdu2222)

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, th

【HDU2222】Keywords Search

Problem DescriptionIn 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 f

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)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

AC自动机 hdu2222

1 #include <iostream> 2 using namespace std; 3 4 struct Node{ 5 Node *next[26]; 6 Node* fail; 7 int count; 8 Node(){ 9 for (int i = 0; i < 26; i++){ 10 next[i] = NULL; 11 } 12 fail = NULL; 13 count = 0; 14 } 15 }; 16 char words[51], s[1000001]; 1

HDU2222 AC自动机

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