Keywords Search(ac自动机

本文介绍了如何使用C++实现字符串插入操作,并利用自动机(Automata)构建AC自动机进行高效查询。通过AC自动机,作者展示了如何在大规模数据中快速查询字符串出现频率。涉及的主要技术包括字符数组、队列和失败链接算法。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;

namespace AC {
    int tr[N][26], tot;
    int e[N], fail[N];
    void insert(char *s) {
        int u = 0;
        for (int i = 1; s[i]; i++) {
            if (!tr[u][s[i] - 'a']) tr[u][s[i] - 'a'] = ++tot;
            u = tr[u][s[i] - 'a'];
        }
        e[u]++;
    }
    queue<int> q;
    void build() {
        for (int i = 0; i < 26; i++)
            if (tr[0][i]) q.push(tr[0][i]);
        while (q.size()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < 26; i++) {
                if (tr[u][i])
                    fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]);
                else
                    tr[u][i] = tr[fail[u]][i];
            }
        }
    }
    int query(char *t) {
        int u = 0, res = 0;
        for (int i = 1; t[i]; i++) {
            u = tr[u][t[i] - 'a'];  // 转移
            for (int j = u; j && e[j] != -1; j = fail[j]) {
                res += e[j], e[j] = -1;
            }
        }
        return res;
    }
    void init(){
        memset(e,0,sizeof e);
        memset(fail,0,sizeof fail);
        memset(tr,0,sizeof tr);
        tot=0;
    }
}  // namespace AC


char s[N];int T;
int main() {
   scanf("%d",&T);
   while(T--){
        AC::init();
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%s", s + 1), AC::insert(s);
        scanf("%s", s + 1);
        AC::build();
        printf("%d\n", AC::query(s));
        // return 0;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值