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): 22721    Accepted Submission(s): 7553

    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<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=26;
    
    struct Trie{
        Trie *fail;     //失败指针
        Trie *next[N];   //Tire每个节点的26个子节点(最多26个字母)
        int count;       //是否为该单词的最后一个节点
    
        Trie(){     //构造函数初始化
            fail=NULL;
            count=0;
            memset(next,NULL,sizeof(next));
        }
    }*q[500010];    //队列,方便用于bfs构造失败指针
    
    char word[55];  //输入的单词
    char str[1000010];   //模式串
    int head,tail;      //队列的头尾指针
    
    void Insert(char *str,Trie *Root){
        Trie *loc=Root;
        int i=0;
        while(str[i]!='\0'){
            int id=str[i]-'a';
            if(loc->next[id]==NULL)
                loc->next[id]=new Trie();
            loc=loc->next[id];
            i++;
        }
        loc->count++;
    }
    
    void AC_automation(Trie *Root){
        Root->fail=NULL;
        q[head++]=Root;
        Trie *cur,*tmp;
        while(head!=tail){
            cur=q[tail++];
            tmp=NULL;
            for(int i=0;i<26;i++){
                if(cur->next[i]!=NULL){
                    if(cur==Root)
                        cur->next[i]->fail=Root;
                    else{
                        tmp=cur->fail;
                        while(tmp!=NULL){
                            if(tmp->next[i]!=NULL){
                                cur->next[i]->fail=tmp->next[i];
                                break;
                            }
                            tmp=tmp->fail;
                        }
                        if(tmp==NULL)
                            cur->next[i]->fail=Root;
                    }
                    q[head++]=cur->next[i];
                }
            }
        }
    }
    
    int query(Trie *Root){
        int i=0,cnt=0;
        Trie *loc=Root,*tmp;
        while(str[i]!='\0'){
            int id=str[i]-'a';
            while(loc->next[id]==NULL && loc!=Root)
                loc=loc->fail;
            loc=loc->next[id];
            loc=(loc==NULL)?Root:loc;
            tmp=loc;
            while(tmp!=Root && tmp->count!=-1){
                cnt+=tmp->count;
                tmp->count=-1;
                tmp=tmp->fail;
            }
            i++;
        }
        return cnt;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        scanf("%d",&t);
        while(t--){
            head=tail=0;
            Trie *Root=new Trie();
            scanf("%d",&n);
            getchar();
            while(n--){
                gets(word);
                Insert(word,Root);
            }
            AC_automation(Root);
            scanf("%s",str);
            printf("%d\n",query(Root));
        }
        return 0;
    }
  • 相关阅读:
    设计模式 创建型 抽象工厂
    mysql 案例 ~ 分析执行完的大事务
    mysql 查询优化案例汇总
    mysql 原理 ~ innodb恢复机制
    mysql 原理~ 乐观锁和悲观锁
    mysql 原理 ~ 常规锁
    mysql 5.7 ~ 新特性
    mysql 原理 ~ checkpoint
    mysql原理~undo管理
    mysql原理~undo
  • 原文地址:https://www.cnblogs.com/jackge/p/2992368.html
Copyright © 2011-2022 走看看