hdu 2222 Keywords Search(字符串匹配-AC自动机)

Keywords Search

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


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
 
AC自动机模板题。
建议看博客: http://www.cppblog.com/mythit/archive/2009/04/21/80633.html

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct Tri{
    Tri *next[26] , *fail;
    int sum;
    Tri(){
        for(int i = 0; i < 26; i++){
            next[i] = NULL;
        }
        fail = NULL;
        sum = 0;
    }
};
vector keywords;
string text;
int N;
Tri *root;

void build(string str){
    int index = 0;
    Tri *node = root;
    while(index < str.length()){
        int c = str[index]-'a';
        if(node->next[c] == NULL){
            node->next[c] = new Tri();
        }
        node = node->next[c];
        index++;
    }
    node->sum++;
}

void bfs(){
    queue q;
    for(int i = 0; i < 26; i++){
        if(root->next[i] != NULL){
            root->next[i]->fail = root;
            q.push(root->next[i]);
        }
    }
    while(!q.empty()){
        Tri *f = q.front();
        q.pop();
        for(int i = 0; i < 26; i++){
            Tri*node = f->fail;
            if(f->next[i] != NULL){
                while(node != NULL && node->next[i] == NULL) node = node->fail;
                if(node == NULL) f->next[i]->fail = root;
                else f->next[i]->fail = node->next[i];
                q.push(f->next[i]);
            }
        }
    }
}

int query(string str){//return the time of keywords appear
    int index = 0;
    Tri *node = root;
    int ans = 0;
    while(index < str.length()){
        int c = str[index] - 'a';
        while(node != NULL && node->next[c] == NULL) node = node->fail;
        if(node == NULL) node = root;
        else node = node->next[c];
        index++;
        Tri *tem = node;
        while(tem != NULL && tem->sum != -1){
            ans += tem->sum;
            tem->sum = -1;
            tem = tem->fail;
        }
    }
    return ans;
}

void initial(){
    keywords.clear();
    text.clear();
    root = new Tri();
}

void readcase(){
    string str;
    scanf("%d" , &N);
    for(int i = 0; i < N; i++){
        cin >> str;
        keywords.push_back(str);
    }
    cin >> text;
}

void computing(){
    for(int i = 0; i < keywords.size(); i++){
        build(keywords[i]);
    }
    bfs();
    cout << query(text) << endl;
}

int main(){
    int T;
    scanf("%d" , &T);
    while(T--){
        initial();
        readcase();
        computing();
    }
    return 0;
}


你可能感兴趣的:(字符串处理-AC自动机)