• 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;
    }
    
    
    
    
    
    
    


  • 相关阅读:
    路由器桥接是个什么玩法
    MAC使用小技巧之------用好mac电脑的10个必知的小技巧!
    学习笔记1--响应式网页+Bootstrap起步+全局CSS样式
    mysql运维必会的一些知识点整理
    面试小结1--填空题
    CSS技术实例1-使用CSS计数器实现数值计算小游戏实例页面
    编译8.0
    解决Windows 10 1809 使用管理员权限运行的程序无法浏览网络驱动器的问题
    android sdk
    酷卓教程 明明已经已经有了面具Magisk 确无法正常使用root权限
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777435.html
Copyright © 2020-2023  润新知