• 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, the system will match the keywords with description of image and show the image which the most keywords be matched. 
    To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match. 

    Input

    First line will contain one integer means how many cases will follow by. 
    Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000) 
    Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50. 
    The last line is the description, and the length will be not longer than 1000000. 

    Output

    Print how many keywords are contained in the description.

    Sample Input

    1
    5
    she
    he
    say
    shr
    her
    yasherhs

    Sample Output

    3

    ac自动机裸题

    ac自动机其实就是kmp的思想 

    在每次匹配失败之后

    通过fail指针找到下一个匹配的节点

    构建fail指针使用bfs不断回找

    这里用new的好处就是new一个对象是会对该对象进行初始化

    开成指针数组比二维数组的好处就是这个最优化该程序所占的内存空间

    每次清空tried树也是必不可少的

    代码

    #define LOCAL
    #define MAXN 1000005
    #include<bits/stdc++.h>
    using namespace std;
    struct node
    {
        node *nxt[30];
        node *fail;
        int cnt=0;
        node()
        {
            cnt=0;
            fail=NULL;
            for(int i=0;i<29;i++) nxt[i]=NULL;
        }
    } *root;
    node* q[MAXN];
    char str[MAXN];
    char tmp[100];
    void Insert()//0->root
    {
        node *u=root;
        for(int i=0;tmp[i];i++)
        {
            int k=tmp[i]-'a';
            if(u->nxt[k]==NULL)
            {
                u->nxt[k]=new node();
            }
            u=u->nxt[k];
        }
        u->cnt++;
        return;
    }
    void bfs()
    {
        int head=1,tail=1;
        q[head]=root;
        node* u;
        node* temp;
        while(head<=tail)
        {
            u=q[head];
            head++;
            for(int i=0;i<26;i++)
            {
                if(u->nxt[i])
                {
                    if(u==root)
                    {
                        u->nxt[i]->fail=root;
                    }
                    else
                    {
                        temp=u->fail;
                        while(temp)
                        {
                            if(temp->nxt[i])
                            {
                                u->nxt[i]->fail=temp->nxt[i];
                                break;
                            }
                            temp=temp->fail;
                        }
                        if(!temp) u->nxt[i]->fail=root;
                    }q[++tail]=u->nxt[i];
                }
    
            }
        }
    }
    void del(node *x)
    {
        if(x==NULL) return ;
        for(int i=0;i<26;i++)
        {
            del(x->nxt[i]);
        }
        delete(x);
    }
    int qurey()
    {
        int ans=0;
        node* u=root;
        node* temp;
        //cout<<"0"<<endl;
        for(int i=0;str[i];i++)
        {
        //cout<<"="<<endl;
           int k=str[i]-'a';
           while(!u->nxt[k]&&u!=root) u=u->fail;
           u=u->nxt[k];
           if(!u) u=root;
           temp=u;
           //cout<<u;
           while(temp!=root&&temp->cnt!=-1)
           {
               //cout<<"2";
               ans+=temp->cnt;
               temp->cnt=-1;
               temp=temp->fail;
           }
        }
        //cout<<"___"<<endl;
        return ans;
    }
    int main()
    {
        #ifdef LOCAL
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
        #endif
        int t;
        scanf("%d",&t);
        while(t--)
        {
            root=new node();
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%s",tmp);
                Insert();
            }
            bfs();
            scanf("%s",str);
            //cout<<"#"<<endl;
            printf("%d
    ",qurey());
            //cout<<"##"<<endl;
            del(root);
        }
    }
    
  • 相关阅读:
    客户端相关知识学习(三)之Android原生与H5交互的实现
    客户端相关知识学习(二)之h5与原生app交互的原理
    nslookup基础用法
    十大渗透测试演练系统
    最新的windows xp sp3序列号(绝对可通过正版验证)
    Metasploit基础命令
    msf回退一步
    验证SMB登入
    Nmap使用指南(1)
    postgreSql基础命令及linux下postgreSql命令
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852211.html
Copyright © 2020-2023  润新知