HDU 2222 - Keywords Search (AC自动机)

本文介绍了一道关于AC自动机的编程题目,旨在帮助读者理解AC自动机的工作原理及其在字符串匹配中的应用。通过具体实例讲解了如何构建AC自动机,并实现高效的关键词搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值