AC自动机-加强版(Fail树,AC自动机)

· · 题解

AC自动机-加强版(Fail树,AC自动机)

题目叙述

给你若干个小字符串和一个大字符串,求这些小字符串在大字符串里出现最多的是哪几个,出现了几次。

题解

Fail树的题目。

可以知道,一个节点x的fail指针指向y,说明出现一次x就一定会出现一次y。那么把给出的字符串在AC自动机上匹配一遍,匹配到的节点标记,然后我们又知道fail树是一个树(废话),所以就可以在树上dp啦(树就是如果fail[a]=b,那么a是b的儿子)。

代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
const int maxNode = 150 * 70 + 5, maxLen = 1e6 + 5;
char small[155][75];
struct AcAutomn {
    int tot, ch[maxNode][26], fail[maxNode], cnt[maxNode], id[maxNode];
    vector<int> tree[maxNode];
    bool tag[maxNode], isMax[155];
    void Init() {
        memset(isMax, 0, sizeof(isMax));
        while (tot) {
            tag[tot] = cnt[tot] = fail[tot] = id[tot] = 0;
            tree[tot].clear();
            memset(ch[tot], 0, sizeof(ch[tot]));
            --tot;
        }
        tot = 1;
    }
    AcAutomn() : tot(1) {}
    void Insert(char *str, int index) {
        int now = 1, len = strlen(str + 1);
        for (int pos = 1; pos <= len; ++pos) {
            if (!ch[now][str[pos] - 'a'])
                ch[now][str[pos] - 'a'] = ++tot;
            now = ch[now][str[pos] - 'a'];
        }
        id[now] = index;
        tag[now] = 1;
    }
    void Build() {
        queue<int> Q;
        while (!Q.empty()) Q.pop();
        fail[1] = 1;
        for (int son = 0; son < 26; ++son) {
            if (ch[1][son]) Q.push(ch[1][son]), fail[ch[1][son]] = 1;
            else ch[1][son] = 1;
            //这里要把儿子的初始值搞好 
        }
        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();
            for (int son = 0; son < 26; ++son) {
                int to = ch[now][son];
                if (to) {
                    fail[to] = ch[fail[now]][son];
                    Q.push(to);
                } else
                    ch[now][son] = ch[fail[now]][son];
            }
        }
    }
    void Match(char *str) {
        int len = strlen(str + 1), now = 1;
        for (int pos = 1; pos <= len; ++pos) {
            now = ch[now][str[pos] - 'a'];
            ++cnt[now];
        }
    }
    void BuildTree() {
        for (int i = 2; i <= tot; ++i) tree[fail[i]].push_back(i);
    }
    void Dfs(int now) {
        for (int to : tree[now]) Dfs(to), cnt[now] += cnt[to];
    }
    int PrintfAns(int nbMo) {
        int ans = 0;
        for (int i = 1; i <= tot; ++i) {
            if (tag[i]) ans = max(ans, cnt[i]);
//          printf("i : %d cnt : %d\n", i, cnt[i]);
        }
        printf("%d\n", ans);
        for (int i = 1; i <= tot; ++i)
            if (tag[i] && ans == cnt[i])
                isMax[id[i]] = 1;
        for (int i = 1; i <= nbMo; ++i)
            if (isMax[i])
                printf("%s\n", small[i] + 1);
        return ans;
    }
    void Check() {
        for (int i = 1; i <= tot; ++i) {
            printf("i : %d fail : %d\n", i, fail[i]);
            for (int son = 0; son < 26; ++son)
                printf("    son : %d node id : %d\n", son, ch[i][son]);
        }
    }
} ac;
int nbMo;
char big[maxLen];
int main() {
    scanf("%d", &nbMo);
    while (nbMo != 0) {
        for (int i = 1; i <= nbMo; ++i) {
            memset(small[i], 0, sizeof(small[i]));
            scanf("%s", small[i] + 1);
            ac.Insert(small[i], i);
        }
        scanf("%s", big + 1);
        ac.Build();
        ac.Match(big);
        ac.BuildTree();
        ac.Dfs(1);
        ac.PrintfAns(nbMo);
        scanf("%d", &nbMo);
        ac.Init();
    }
    return 0;
}

知识点