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

  • 相关阅读:
    笔记(二) C#sql语句
    [叩响C#之门]写给初学者:多线程系列(七)——互锁(Interlocked类)
    C# Async与Await的使用
    C#线程锁使用全功略
    一个C#的加锁解锁示例
    【分析】浅谈C#中Control的Invoke与BeginInvoke在主副线程中的执行顺序和区别(SamWang)
    Control.BeginInvoke()和delegate的BeginInvoke()的区别
    crm04 action操作 和 多级过滤
    VIM和sed 替换字符串方法
    解决Centos关闭You have new mail in /var/spool/mail/root提示(转)
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6401884.html
Copyright © 2011-2022 走看看