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



  • 相关阅读:
    第1次作业
    第0次作业
    总结报告
    第14、15周作业
    第七周作业
    第六周作业
    第四周作业
    第四次作业
    第三次作业
    2018第二次作业
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350940.html
Copyright © 2011-2022 走看看