zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    
  • 相关阅读:
    服务器RAID5阵列掉了两块盘恢复数据过程
    sql server数据库错误数据恢复过程
    raid崩溃导致存储虚拟化平台数据丢失
    详解MBR分区结构以及GPT分区结构
    存储硬件故障如何恢复数据库
    服务器raid常见故障解决方案
    误删除VMware虚拟机vmdk文件的恢复方法
    HP P2000 服务器数据恢复+服务器数据恢复通用办法
    DELL EqualLogic PS6100存储数据丢失的解决方案
    IBM DS4800服务器RAID信息丢失数据恢复方法
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852211.html
Copyright © 2011-2022 走看看