zoukankan      html  css  js  c++  java
  • HDOJ 2222: Keywords Search

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 59360    Accepted Submission(s): 19520


    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   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 

    分析:

    AC自动机模板题...

    代码:

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    //by NeighThorn
    using namespace std;
    
    const int maxn=1000000+5,maxm=50+5; 
    
    int n,cas,tot,head,tail,q[500000+5];
    
    char s[maxn],word[maxm];
    
    struct trie{
      int cnt,fail,nxt[26];
    }tr[500000+5];
    
    inline void init(void){
        for(int i=0;i<=tot;tr[i].fail=-1,tr[i].cnt=0,i++)
            for(int j=0;j<26;j++)
                tr[i].nxt[j]=0;
        tot=0;
    }
    
    inline void insert(char *word){
        int p=0,len=strlen(word);
        for(int i=0;i<len;i++){
            if(!tr[p].nxt[word[i]-'a'])
                tr[p].nxt[word[i]-'a']=++tot;
            p=tr[p].nxt[word[i]-'a'];
        }
        tr[p].cnt++;
    }
    
    inline void buildACM(void){
        head=0,tail=0;q[0]=0;
        while(head<=tail){
            int id=q[head++],p=-1;
            for(int i=0;i<26;i++)
                if(tr[id].nxt[i]){
                    if(id){
                        p=tr[id].fail;
                        while(p!=-1){
                            if(tr[p].nxt[i]){
                                tr[tr[id].nxt[i]].fail=tr[p].nxt[i];
                                break;
                            }
                            p=tr[p].fail;
                        }
                        if(p==-1) tr[tr[id].nxt[i]].fail=0;
                    }
                    else
                        tr[tr[id].nxt[i]].fail=0;
                    q[++tail]=tr[id].nxt[i];
                }
        }
    }
    
    inline int query(void){
        int i=0,ans=0,index,len=strlen(s),p=0;
        while(s[i]){
            index=s[i]-'a';
            while(!tr[p].nxt[index]&&p) p=tr[p].fail;
            p=tr[p].nxt[index];
            int tmp=p;
            while(tmp&&tr[tmp].cnt!=-1)
                ans+=tr[tmp].cnt,tr[tmp].cnt=-1,tmp=tr[tmp].fail;
            i++;
        }
        return ans;
    }
    
    signed main(void){
        scanf("%d",&cas);
        while(cas--){
            tot=500000;init();
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%s",word),insert(word);
            buildACM();
            scanf("%s",s);
            printf("%d
    ",query());
        }
        return 0;
    }
    

      


    By NeighThorn

  • 相关阅读:
    Mysql(二)
    Mysql(一)
    JS图表插件(柱形图、饼状图、折线图)
    如何让Table中的第一列和第二列的值相乘然后赋值给第三列
    js生成验证码并验证
    IIS配置默认文档
    DropDownList如何添加一个空白的选项
    GridView如何合并同类项
    .NET后台如何获取前台HMTL控件的值
    Json数据报错
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6401884.html
Copyright © 2011-2022 走看看