zoukankan      html  css  js  c++  java
  • hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接

    Keywords Search

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


    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
     

    给出n个字符串和一个模式串 问在模式串中出现了多少个给出的字符串

    ac自己主动机模板题

    代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    char str1[55],str2[1111111];
    struct node{
        node *fail,*next[26];
        int ed;
        node(){
            ed=0;
            fail=NULL;
            for(int i=0;i<26;i++)
                next[i]=NULL;
        }
    }*q[511111],*root;
    void tree_insert(char *str){
        node *p=root;
        int l=strlen(str);
        int id;
        for(int i=0;i<l;i++){
            id=str[i]-'a';
            if(p->next[id]==NULL){
                p->next[id]=new node();
            }
            p=p->next[id];
        }
        p->ed++;
    }
    void build_ac_automachine(){
        int i;
        node *temp,*p;
        root->fail=NULL;
        int fr,ed;
        fr=ed=0;
        q[ed++]=root;
        while(fr<ed){
            temp=q[fr++];
            for(i=0;i<26;i++){
                if(temp->next[i]!=NULL){
                    if(temp==root)
                        temp->next[i]->fail=root;
                    else {
                        p=temp->fail;
                        while(p){
                            if(p->next[i]){
                                temp->next[i]->fail=p->next[i];
                                break;
                            }
                            p=p->fail;
                        }
                        if(p==NULL)
                            temp->next[i]->fail=root;
                    }
                    q[ed++]=temp->next[i];
                }
            }
        }
    }
    int query(char *str){
        int cnt=0;
        int l=strlen(str);
        node *p=root;
        for(int i=0;i<l;i++){
            int id=str[i]-'a';
            while(p->next[id]==NULL&&p!=root)
                p=p->fail;
            p=p->next[id];
            if(p==NULL)
                p=root;
            node *temp=p;
            while(temp!=root&&temp->ed!=-1){
                cnt+=temp->ed;
                temp->ed=-1;
                temp=temp->fail;
            }
        }
        return cnt;
    }
    int main(){
        int t,n;
        scanf("%d",&t);
        while(t--){
            root=new node();
            scanf("%d",&n);
            while(n--){
                scanf("%s",str1);
                tree_insert(str1);
            }
            scanf("%s",str2);
            build_ac_automachine();
            printf("%d
    ",query(str2));
        }
        return 0;
    }


  • 相关阅读:
    2018福大软工实践第二次结对作业
    2018福大软工实践第一次结对作业
    《构建之法》第三章读书笔记
    《构建之法》第八章读书笔记
    Beta版本冲刺前准备
    项目测评
    Alpha版本事后诸葛亮
    Alpha版本冲刺(十)
    Alpha版本冲刺(九)
    Alpha版本冲刺(八)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5220258.html
Copyright © 2011-2022 走看看