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



  • 相关阅读:
    [概述]移动机器人自主探索
    MRPT编译
    Kinect2.0相机标定
    小豆包的学习之旅:里程计运动模型
    小豆包的学习之旅:入门篇
    Kinect2.0点云数据获取
    COFF,amd64.vc90.mfc两个布署的问题
    [硬件]Robot运动控制
    [硬件]Urg_viewer数据读取
    [硬件]三维点云数据获取
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350940.html
Copyright © 2011-2022 走看看