码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-2222 AC自动机模板题

时间:2018-02-12 22:59:39      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:googl   ber   cto   user   fail   match   ret   pac   some   

Keywords Search

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


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
 
废话:今天新学习了AC自动机,自己写的模板一发过,美滋滋
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cctype>
#include <cassert>
#include <bitset>
#include <ctime>

using namespace std;

#define pau system("pause")
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define clr(a, x) memset(a, x, sizeof(a))

const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

int T, n, ans;
char s[10015], t[1000015];
struct trie {
    char c;
    int vis, is_end, index;
    int fail;
    trie *nex[26];
} *root;
trie *create_node(char c) {
    trie *T = new trie;
    T -> c = c;
    T -> vis = T -> is_end = T -> index = 0;
    T -> fail = 0;
    for (int i = 0; i < 26; ++i) {
        T -> nex[i] = NULL;
    }
    return T;
}
void Insert(int l) {
    trie *tT = root;
    for (int i = 1; i <= l; ++i) {
        int x = s[i] - a;
        if (NULL == tT -> nex[x]) {
            tT -> nex[x] = create_node(s[i]);
        }
        tT = tT -> nex[x];
        if (i == l) {
            ++tT -> is_end;
        }
    }
}
trie *arr[261111];
int index;
void get_fail() {
    queue<int> que;
    que.push(1);
    arr[1] = root;
    while (que.size()) {
        int x = que.front(); que.pop();
        for (int i = 0; i < 26; ++i) {
            if (NULL == arr[x] -> nex[i]) continue;
            arr[++index] = arr[x] -> nex[i];
            arr[x] -> nex[i] -> index = index;
            int y = arr[x] -> fail;
            while (y && NULL == arr[y] -> nex[i]) {
                y = arr[y] -> fail;
            }
            if (!y) {
                arr[index] -> fail = 1;
            } else {
                arr[index] -> fail = arr[y] -> nex[i] -> index;
            }
            que.push(index);
        }
    }
}
void go() {
    trie *tT = root;
    for (int i = 1; t[i]; ++i) {
        int x = t[i] - a;
        while (tT != root && NULL == tT -> nex[x]) {
            tT = arr[tT -> fail];
        }
        if (root != tT || tT -> nex[x]) {
            tT = tT -> nex[x];
        }
        tT -> vis = 1;
    }
    for (int i = 1; i <= index; ++i) {
        if (arr[i] -> vis) {
            int y = arr[i] -> fail;
            while (y && !arr[y] -> vis) {
                arr[y] -> vis = 1;
                y = arr[y] -> fail;
            }
        }
    }
    for (int i = 1; i <= index; ++i) {
        ans += arr[i] -> vis * arr[i] -> is_end;
    }
}
int main() {
    scanf("%d", &T);
    while (T--) {
        root = create_node(#);
        ans = 0, index = 1;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%s", s + 1);
            int l = strlen(s + 1);
            Insert(l);
        }
        get_fail();
        /*for (int i = 1; i <= index; ++i) {
            printf("arr[%d] c = %c, is_end = %d, fail = %d\n", i, arr[i] -> c, arr[i] -> is_end, arr[i] -> fail);
        }*/
        scanf("%s", t + 1);
        go();
        printf("%d\n", ans);
    }
    return 0;
}

 

hdu-2222 AC自动机模板题

标签:googl   ber   cto   user   fail   match   ret   pac   some   

原文地址:https://www.cnblogs.com/BIGTOM/p/8445714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!