zoukankan      html  css  js  c++  java
  • HDOJ 2222 Keywords Search

    ac自动机模板题

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 25993    Accepted Submission(s): 8464


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

    Author
    Wiskey
     

    Recommend
    lcy
     


    #include <iostream>
    #include <cstring>
    #include <cstdio>

    using namespace std;

    const int kind=26;

    struct node
    {
        node* fail;
        node* next[kind];
        int cont;
        node()
        {
            fail=NULL;
            cont=0;
            memset(next,0,sizeof(next));
        }
    }*q[500001];

    char keyworld[51];
    char str[1000001];

    int head,tail;

    void insert(char*str,node* root)
    {
        node *p=root;
        int i=0,index;
        while(str)
        {
            index=str-'a';
            if(p->next[index]==NULL) p->next[index]=new node();
            p=p->next[index];
            i++;
        }
        p->cont++;
    }

    void build_ac(node* root)
    {
        root->fail=NULL;
        q[head++]=root;
        while(head!=tail)
        {
            node* temp=q[tail++];
            node* p=NULL;

            for(int i=0;i<26;i++)
            {
                if(temp->next!=NULL)
                {
                    if(temp==root)
                        temp->next->fail=root;
                    else
                    {
                        p=temp->fail;
                        while(p!=NULL)
                        {
                            if(p->next!=NULL)
                            {
                                temp->next->fail=p->next;
                                break;
                            }
                            p=p->fail;
                        }
                        if(p==NULL)
                            temp->next->fail=root;
                    }
                    q[head++]=temp->next;
                }
            }
        }
    }

    int query(node* root)
    {
        int i=0,cnt=0,index;
        node* p=root;

        while(str)
        {
            index=str-'a';
            while(p->next[index]==NULL&&p!=root)  p=p->fail;
            p=p->next[index];
            p=(p==NULL)?root:p;
            node* temp=p;
            while(temp!=root&&temp->cont!=-1)
            {
                cnt+=temp->cont;
                temp->cont=-1;
                temp=temp->fail;
            }
            i++;
        }

        return cnt;
    }

    int main()
    {
        int n,T;
        scanf("%d",&T);
    while(T--)
    {
        head=tail=0;
        node* root=new node();
        scanf("%d",&n);
        getchar();
        while(n--)
        {
            gets(keyworld);
            insert(keyworld,root);
        }
        build_ac(root);
        scanf("%s",str);

        printf("%d ",query(root));
    }

        return 0;
    }



  • 相关阅读:
    理解Javascript_13_执行模型详解
    vs2005的快捷键
    去除 VS.Net 2003 项目的 VSS 息的脚本
    VS2005的隐藏快捷键
    程序员,你离坐牢还有多远
    对路径XXX的访问被拒绝(文件操作权限)的解决方法
    安装VS2005 SP1之后无法更改或卸载VS2005的处理方法
    强大的.NET反编译工具Reflector及插件
    反编译工具Reflector下载(集成两个常用.net插件,FileGenerator和FileDisassembler)
    两种彻底删除VIEWSTATE的方法
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350940.html
Copyright © 2011-2022 走看看