HDU - 2222 Keywords Search AC自动机

  

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
1
5
she
he
say
shr
her
yasherhs
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值