AC自动机模板

知识点

  1. 视频讲解
  2. 文章讲解
    在这里插入图片描述

LuoguP3808【模板】AC 自动机(简单版)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n;
char s[N];
namespace AC
{
int tr[N][26], tot;
int e[N], fail[N];          //e[i]  表示以 i 这个节点编号位置为结尾的单词有多少个

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;    //注意这里必须用前置++ ,因为默认根节点的编号为 0
        u = tr[u][s[i] - 'a'];
    }
    e[u] ++;                //u 这个编号结尾的单词数量 ++
}

void build()                //bfs 构建 fail 指针
{
    queue<int> q;
    int u = 0;
    for (int i = 0; i < 26; i ++)
    {
        if (tr[u][i])
            q.push(tr[u][i]);
    }

    while (q.size())
    {
        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 s[])
{
    int u = 0, res = 0;
    for (int i = 1; s[i]; i ++)
    {
        u = tr[u][s[i] - 'a'];
        for (int j = u; j && e[j] != -1; j = fail[j])
        {
            res += e[j]; 
            e[j] = -1;
        }
    }

    return res;
}
}



int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++)
    {
        scanf("%s", s + 1);
        AC::insert(s);
    }
    AC::build();
    scanf("%s", s + 1);
    int ans = AC::query(s);
    printf("%d\n", ans);

    return 0;
}

P3796【模板】AC 自动机 (加强版)

#include <bits/stdc++.h>
using namespace std;
const int N = 155;
const int M = 1e6 + 10;
char s[N][N], t[M];
int n;
namespace AC
{
const int SZ = N * 80;    
int tr[SZ][26], tot;
int fail[SZ], idx[SZ], val[SZ];
int cnt[N];
void init()
{
    memset(tr, 0, sizeof tr);
    memset(fail, 0, sizeof fail);
    memset(idx, 0, sizeof idx);
    memset(val, 0, sizeof val);
    memset(cnt, 0, sizeof cnt);
    tot = 0;                        //不要忘了初始化
}

void insert(char s[], int id)
{
    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'];
    }
    idx[u] = id;
}

void build()
{
    queue<int> q;
    int u = 0;
    for (int i = 0; i < 26; i ++)
    {
        if (tr[u][i])
            q.push(tr[u][i]);
    }

    while (q.size())
    {
        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 s[])
{
    int u = 0;
    for (int i = 1; s[i]; i ++)
    {
        u = tr[u][s[i] - 'a'];
        for (int j = u; j; j = fail[j])
            val[j] ++;
    }

    int res = 0;                        //模式串的最大出现次数
    for (int i = 0; i <= tot; i ++)
    {
        if (idx[i]) res = max(res, val[i]), cnt[idx[i]] = val[i];
    }
    return res;
}
}


int main()
{
    while (scanf("%d", &n) && n)
    {
        AC::init();
        for (int i = 1; i <= n; i ++)
        {
            scanf("%s", s[i] + 1);
            AC::insert(s[i], i);
        }
        AC::build();
        scanf("%s", t + 1);
        int ans = AC::query(t);
        printf("%d\n", ans);
        for (int i = 1; i <= n; i ++)
        {
            if (AC::cnt[i] == ans)
                printf("%s\n", s[i] + 1);
        }
    }


    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值