HDU - 2222 Keywords Search AC自动机

来源:互联网 发布:淘宝3000多的实体娃娃 编辑:程序博客网 时间:2024/05/20 01:10

  

Keywords Search

 HDU - 2222


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


Source
HDU - 2222
My Solution
题意:给出n个字符串为这些字符串在主串s中出现的个数。
AC自动机
裸的AC自动机,注意下给定模式串可能有一些相同的串,然后按照主串在自动机上遍历即可。
复杂度 O(n)
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#include <map>using namespace std;typedef long long LL;const int CHAR_SIZE = 26;const int MAX_SIZE = 5e5 + 8;map<char, int> mp;struct AC_Machine{    int ch[MAX_SIZE][CHAR_SIZE], danger[MAX_SIZE], fail[MAX_SIZE];    int sz;    inline void init(){        sz = 1;        memset(ch[0], 0, sizeof ch[0]);        memset(danger, 0, sizeof danger);    }    inline void _insert(const string &s){        int n = s.size();        int u = 0, c;        for(int i = 0; i < n; i++){            c = mp[s[i]];            if(!ch[u][c]){                memset(ch[sz], 0, sizeof ch[sz]);                danger[sz] = 0;                ch[u][c] = sz++;            }            u = ch[u][c];        }        danger[u]++;    }    inline void _build(){        queue<int> Q;        fail[0] = 0;        for(int c = 0, u; c < CHAR_SIZE; c++){            u = ch[0][c];            if(u){Q.push(u); fail[u] = 0;}        }        int r;        while(!Q.empty()){            r = Q.front();            Q.pop();            //danger[r] |= danger[fail[r]];            for(int c = 0, u; c < CHAR_SIZE; c++){                u = ch[r][c];                if(!u){ch[r][c] = ch[fail[r]][c]; continue; }                fail[u] = ch[fail[r]][c];                Q.push(u);            }        }    }}ac;string s;int main(){    #ifdef LOCAL    freopen("3.in", "r", stdin);    //freopen("3.out", "w", stdout);    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    int T, n, len, now, i, ans, tmp;    for(int i = 0; i < 26; i++){mp[(i + 'a')] = i;}    cin >> T;    while(T--){        cin >> n;        ac.init();        while(n--){            cin >> s;            ac._insert(s);        }        ac._build();        cin >> s;        ans = 0; len = s.size(); now = 0;        for(i = 0; i < len; i++){            now = ac.ch[now][mp[s[i]]];            tmp = now;            while(tmp){                if(ac.danger[tmp]){                    ans += ac.danger[tmp];                    ac.danger[tmp] = 0;                }                tmp = ac.fail[tmp];            }        }        cout << ans << "\n";    }    return 0;}


Thank you!
                                                                                                                                             ------from ProLights

0 0
原创粉丝点击