杭电2222 Keywords Search(AC自动机)

Keywords Search

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


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
/*
迷迷糊糊套模板。。加油!!!
Time:2015-05-02
*/
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=500000+10;
struct Trie{
    int next[MAX][26],fail[MAX],end[MAX];
    int L,rt;
    int NewNode(){
        for(int i=0;i<26;i++){
            next[L][i]=-1;
        }end[L++]=0;
        return L-1;
    }
    void Init(){
        L=0;rt=NewNode();
    }
    void Insert(char *buf){
        int len=strlen(buf);
        int cur=rt;
        for(int i=0;i<len;i++){
            int id=buf[i]-'a';
            if(next[cur][id]==-1){
                next[cur][id]=NewNode();
            }
            cur=next[cur][id];
        }
        end[cur]++;
    }
    void Build(){
        queue<int>q;
        int cur=rt;
        for(int i=0;i<26;i++){
            if(next[cur][i]==-1){
                next[cur][i]=rt;
            }else{
                fail[next[cur][i]]=rt;
                q.push(next[cur][i]);
            }
        }
        while(!q.empty()){
            cur=q.front();q.pop();
            for(int i=0;i<26;i++){
                if(next[cur][i]==-1){
                    next[cur][i]=next[fail[cur]][i];
                }else{
                    fail[next[cur][i]]=next[fail[cur]][i];
                    q.push(next[cur][i]);
                }
            }
        }
    }
    int Query(char *buf){
        int len=strlen(buf);
        int cur=rt;
        int ans=0;
        for(int i=0;i<len;i++){
            cur=next[cur][buf[i]-'a'];
            int tmp=cur;
            while(tmp!=rt){
                ans+=end[tmp];
                end[tmp]=0;
                tmp=fail[tmp];
            }
        }
        return ans;
    }
};
Trie AC;
int main(){
    int T,n;
    char str[MAX<<1];
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        AC.Init();
        for(int i=0;i<n;i++){
            scanf("%s",str);
            AC.Insert(str);
        }
        AC.Build();
        scanf("%s",str);
        printf("%d\n",AC.Query(str));
    }
return 0;
}

猜你喜欢

转载自blog.csdn.net/u013634213/article/details/45439053