hdu 2222 Keywords Search(AC自动机)

本文介绍了一道AC自动机的经典练习题目,详细展示了如何通过构建AC自动机来解决字符串匹配问题,并提供了完整的代码实现,包括初始化、添加模式串、构建失败指针及搜索算法等关键步骤。

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

AC自动机练手题。注意题意,keyword可能重复,这些重复的keyword要单独计算。

PS:升级了AC自动机模板 O(∩_∩)O~ 感觉更和谐了


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))

typedef long long LL;

const int inf  = 0x7fffffff;
const int Maxn = 100000;

struct ACAutomation {
    // 最大节点数:模式串个数 * 最大串长度
    static const int MAX_NODE = 10000 * 50 + 50;
    static const int CHAR_SET = 26;
    static const int BASE     = 'a';
    int n;                          // trie大小
    int id[BASE+CHAR_SET+1];        // 字符->int
    int tag[MAX_NODE];              // 标记,根据需要调整
    int last[MAX_NODE];             // 后缀链接,链接到上一个单词节点
    int fail[MAX_NODE];
    int trie[MAX_NODE][CHAR_SET];

    void init() {
        for (int i=0;i<CHAR_SET;++i)
            id[BASE+i] = i;
        n = 1;
        memset(trie[0], -1, sizeof(trie[0]));
        memset(tag, 0, sizeof(tag));
        memset(last, 0, sizeof(last));
    }

    void add(const char *s) {
        int p = 0;
        while (*s) {
            int i = id[*s];
            if (trie[p][i] == -1) {
                memset(trie[n], -1, sizeof(trie[n]));
                trie[p][i] = n++;
            }
            ++s;
            p = trie[p][i];
        }
        //tag[p] = 1;
        tag[p]++;
    }

    void build() {
        queue<int> Q;
        fail[0] = 0;
        int u, v;
        for (int i=0;i<CHAR_SET;++i) {
            u = trie[0][i];
            if (u != -1) {
                fail[u] = 0;
                Q.push(u);
            }
            else
                trie[0][i] = 0;
        }

        while (!Q.empty()) {
            int fr = Q.front();Q.pop();
            for (int i=0;i<CHAR_SET;++i) {
                int &u = trie[fr][i];    // 注意这里使用了一个引用
                if ( u != -1 ) {
                    Q.push(u);
                    fail[u] = trie[fail[fr]][i];
                    //tag[u] += tag[fail[u]];
                    last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
                }
                else
                    u = trie[fail[fr]][i];
            }
        }
    }

    int search(const char * T, int len) {
        int p = 0;
        int ret = 0;
        for (int i=0;i<len;++i) {
            //cout << "on " << T[i] << endl;
            if (T[i] < 'a' || T[i] > 'z') {
                p = 0;
                continue;
            }
            int c = id[T[i]];
            //hile (p && !trie[p][c]) p = fail[p];
            p = trie[p][c];
            //ret += tag[p];
            int v = last[p];
            if (tag[p]) {ret += tag[p];tag[p] = 0;}
            while (v) {
                if (tag[v]) {
                    ret += tag[v];
                    tag[v] = 0;
                }
                v = last[v];
            }
            //cout << "match on node: " << p << " cnt:  " << ret << endl;
        }
        return ret;
    }
};

string text;
ACAutomation ac;

int main() {
#if 1
    freopen("input.in", "r", stdin);
#endif
    iostream::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--) {
        ac.init();
        int n;
        cin >> n;
        string str;
        while (n--) {
            cin >> str;
            ac.add(str.c_str());
        }
        cin >> text;
        ac.build();
        cout << ac.search(text.c_str(), text.length()) << endl;;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值