HDU 2222 - Keywords Search (AC自动机)

来源:互联网 发布:单片机是ic吗 编辑:程序博客网 时间:2024/04/30 22:17

Keywords Search

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


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   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341

题意:

求目标串中出现了几个模式串。



AC自动机模板题


#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int MAX_NODE = 500000 + 20;const int SIGMA_SIZE = 26;struct ACAutomata {    int ch[MAX_NODE][SIGMA_SIZE];    int val[MAX_NODE];    int last[MAX_NODE];    int f[MAX_NODE];    int sz;    ACAutomata() {        init();    }    void init() {        sz = 1;        memset(ch[0], 0, sizeof(ch[0]));    }    int idx(char c) { return c - 'a'; }    void insert(char * s) {        int u = 0, n = strlen(s);        for (int i=0; i<n; i++) {            int c = idx(s[i]);            if (!ch[u][c]) {                memset(ch[sz], 0, sizeof(ch[sz]));                val[sz] = 0;                ch[u][c] = sz++;            }            u = ch[u][c];        }        val[u]++;    }    int build() {        queue<int> Q;        f[0] = 0;        for (int c=0; c<SIGMA_SIZE; c++) {            int u = ch[0][c];            if (u) { f[u] = 0; Q.push(u); last[u] = 0; }        }        while(!Q.empty()) {            int r = Q.front(); Q.pop();            for (int c = 0; c < SIGMA_SIZE; c++) {                int u = ch[r][c];                if (!u) continue;                Q.push(u);                int v = f[r];                while (v && !ch[v][c]) v = f[v];                f[u] = ch[v][c];                last[u] = val[f[u]] ? f[u] : last[f[u]];            }        }    }    int print(int i, int j) {        if(j) {            //printf("%d: %d\n", j, val[j]);            int t = val[j];            val[j] = 0;            return print(i, last[j]) + t;        }        return 0;    }    int find(char * T) {        int ret = 0;        int n = strlen(T);        int j = 0;        for (int i = 0; i < n; i++) {            int c = idx(T[i]);            while (j && !ch[j][c]) j = f[j];            j = ch[j][c];            if(val[j]) ret += print(i, j);            else if(last[j]) ret += print(i, last[j]);        }        return ret;    }};ACAutomata ac;char s[1000000 + 20];int main() {    int T;    scanf("%d", &T);    while (T--) {        int n;        ac.init();        scanf("%d", &n);        for (int i=0; i<n; i++) {            scanf("%s", s);            ac.insert(s);        }        ac.build();        scanf("%s", s);        int ans = ac.find(s);        printf("%d\n", ans);    }    return 0;}



0 0
原创粉丝点击