zoukankan      html  css  js  c++  java
  • HDU2222【AC自动机(基础·模板)】

    Frist AC zi dong ji(Aho-Corasick Automation) of life

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N=5e5+10;    //10000个串,长度为50 
    
    struct Trie{
        int num;
        Trie *next[27],*fail;
    };
    Trie q[N],*root;
    int tol;
    
    Trie* Creat()
    {
        Trie *p;
        p=&q[tol++];
        p->fail=NULL;
        p->num=0;
        for(int i=0;i<26;i++)
            p->next[i]=NULL;
        return p;
    }
    
    void Insert(char *str)
    {
        Trie *p=root;
        int index,len=strlen(str);
        for(int i=0;i<len;i++)
        {
            index=str[i]-'a';
            if(p->next[index]==NULL)
                p->next[index]=Creat();
            p=p->next[index];
        }
        p->num++;
    }
    
    void Build_Ac()
    {
        queue<Trie*>que;
        que.push(root);
        while(!que.empty())
        {
            Trie *p=que.front();que.pop();
            for(int i=0;i<26;i++)
            {
                if(p->next[i]!=NULL)
                {
                    if(p==root)
                        p->next[i]->fail=root;
                    else
                    {
                        Trie *temp;
                        temp=p->fail;
                        while(temp!=NULL)
                        {
                            if(temp->next[i]!=NULL)
                            {
                                p->next[i]->fail=temp->next[i];
                                break;
                            }
                            temp=temp->fail;
                        }
                        if(temp==NULL)
                            p->next[i]->fail=root;
                    }
                    que.push(p->next[i]);
                }
            }
        }
    }
    
    char word[1000010];
    int Query()
    {
        int ans,index,len;
        Trie *p=root;
        ans=0;
        len=strlen(word);
        for(int i=0;i<len;i++)
        {
            index=word[i]-'a';
            while(p->next[index]==NULL && p!=root)//失配跳转到失败指针 
                p=p->fail;
            p=p->next[index];
            if(p==NULL)    //还是失配 
                p=root;
            Trie *temp=p;    //p不动,temp计算后缀串 
            while(temp!=root && temp->num!=-1)
            {
                ans+=temp->num;
                temp->num=-1;
                temp=temp->fail;
            }
        }
        return ans;
    }
    
    int main()
    {
        char s[51];
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n;
            tol=0;
            root=Creat();
            scanf("%d",&n);
            while(n--)
            {
                scanf("%s",s);
                Insert(s);    
            }
            Build_Ac();
            scanf("%s",word);
            printf("%d
    ",Query());
        }
        return 0;
    }
    
    
    
    
    
    
    


  • 相关阅读:
    Redis哨兵(Sentinel)模式
    一个http请求就是一个线程吗,java的服务是每收到一个请求就新开一个线程来处理吗
    Redis 快速入门
    Redis 持久化之RDB和AOF
    Junit 入门使用教程 转自:http://www.cnblogs.com/ysocean/p/6889906.html
    Spring里PropertyPlaceholderConfigurer类的使用 转自:https://www.cnblogs.com/huqianliang/p/5673701.html
    Apache Commons Codec 编码/解码 (Base64/MD5/SHA1/SHA256等算法) 转自https://blog.csdn.net/hbtj_1216/article/details/52813741
    hive中时间日期函数的使用
    关于mat函数
    strip 和split
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777435.html
Copyright © 2011-2022 走看看