HDU-2222 Keywords Search(AC自动机)

Keywords Search

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


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

 题意大概就是问模式串在主串中出现了几次,要注意的是模式串可能会重复,重复的也要算一个

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;

const int N = 1000000 * 5;
const int maxn = 26;
struct Aho_Corasick{
    int next[N][maxn], fail[N], data[N];
    int root, sz, cnt;
    int newnode(){
        for(int i = 0; i < maxn; i++) next[sz][i] = -1;
        data[sz] = 0;
        return sz++;
    }
    void Init() {cnt = sz = 0; root = newnode(); }
    int lx(char c) { return c-'a';}
    void Insert(char *str, int v){
        int len = strlen(str), u = root, c;
        for(int i = 0; i < len; i++){
            c = lx(str[i]);
            if(next[u][c] == -1)
                next[u][c] = newnode();
            u = next[u][c];
        }
        data[u]++;
    }
    void Build(){
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < maxn; i++){
            if(next[root][i] == -1) next[root][i] = root;
            else{
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        }
        while(!Q.empty()){
            int u = Q.front(); Q.pop();
            for(int i = 0; i < maxn; i++){
                if(next[u][i] == -1) next[u][i] = next[fail[u]][i];
                else{
                    fail[next[u][i]] = next[fail[u]][i];
                    Q.push(next[u][i]);
                }
            }
        }
    }
    void Query(char *str, int v){
        int len = strlen(str), u = root, c;
        for(int i = 0; i < len; i++){
            c = lx(str[i]);
            u = next[u][c];
            int tmp = u;
            while(tmp != root){
                if(data[tmp] != -1){
                    cnt += data[tmp];
                    data[tmp] = -1;
                }
                tmp = fail[tmp];
            }
        }
    }
    int ANS(){ return cnt;}

}AC;

char str[1000005];

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        AC.Init();
        scanf("%d", &n);
        for(int i = 1; i <=n; i++){
            scanf("%s", str);
            AC.Insert(str, i);
        }
        AC.Build();
        scanf("%s", str);
        AC.Query(str, n);
        printf("%d\n", AC.ANS());
    }
}

 

posted @ 2017-08-22 19:28  Pretty9  阅读(186)  评论(0编辑  收藏  举报