Keywords Search

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


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
15shehesayshrheryasherhs
 
Sample Output
3
 

Author
Wiskey
 

Recommend
lcy

题意:n个单词,一个文本pattern长度m,问有多少个单词初选在pattern中

kmp复杂度O(len n*(len n+m))

ac自动机(totlen+m);

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
#define debug puts
#define fst ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e6+6;
typedef long long ll;
struct node{
    node *next[26];
    node *fail;
    int flag;
    node(){
        for(int i=0;i<26;i++)   next[i]=NULL;
        fail=NULL;
        flag=0;
    }
};
node *root;
node *q[N];
char str[N],pattern[N];
int ans;

void buildtrie(){
    node *p=root;
    for(int i=0;i<(int)strlen(str);++i){
        int num=str[i]-'a';
        if(p->next[num]==NULL){
            p->next[num]=new node();
        }
        p=p->next[num];
    }
    p->flag++;
}

void build_fail_pointer(){
    int head=0,tail=1;
    q[head]=root;
    node *p;
    node *temp;
    while(head<tail){
        temp=q[head++];
        for(int i=0;i<=25;i++){
            if(!temp->next[i])  continue;
            if(temp->next[i]){
                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)  temp->next[i]->fail=root;
                }
            }
            q[tail++]=temp->next[i];
        }
    }
}

void ac_automation(){
    node *p=root;
    int len=(int)strlen(pattern);
    for(int i=0;i<len;i++){

        int num=pattern[i]-'a';
        while(p->next[num]==NULL&&p!=root)
            p=p->fail;
        p=p->next[num];
        if(!p)  p=root;
        node *temp=p;
        while(temp!=root){
            if(temp->flag>=0){
                ans+=temp->flag;
                temp->flag=-1;
            }
            else    break;
            temp=temp->fail;
        }
    }
}

int main(void){
    int t;
    cin >>t;
    while(t--){
        int n;
        scanf("%d",&n);
        getchar();
        root=new node();
        for(int i=1;i<=n;i++){
            gets(str);
            buildtrie();
        }
        ans=0;
        gets(pattern);
        build_fail_pointer();
        ac_automation();
        printf("%d\n",ans);
    }
    return 0;
}