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



  • 相关阅读:
    servlet的监听器、过滤器、拦截器的区别
    根据一个单词找所有的兄弟单词的思想如何处理
    Maven deploy 部署 jar 到 Nexus 私服
    在vps上搭建hexo博客
    Java解决LeetCode72题 Edit Distance
    学以致用:Python爬取廖大Python教程制作pdf
    puppeteer截图
    Netty接收HTTP文件上传及文件下载
    Docker中执行Shell出现乱码
    Netty URL路由方案探讨
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350940.html
Copyright © 2020-2023  润新知