AC自动机(模板)

首先要明白AC自动机是干什么的:

AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法。下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问你这个单词是否在这个字符串中出现过(匹配),这个问题可以用kmp算法在比较高效的效率上完成这个任务。那么现在我们换个问题,给你很多个单词,然后给你一段字符串,问你有多少个单词在这个字符串中出现过,当然我们暴力做,用每一个单词对字符串做kmp,这样虽然理论上可行,但是时间复杂度非常之高,当单词的个数比较多并且字符串很长的情况下不能有效的解决这个问题,所以这时候就要用到我们的ac自动机算法了。

一个母字符串,多个子字符串与其多次匹配,就是它的用处。

这里有很多例题:AC自动机小结

例题1:HDU2222

//======================
// HDU 2222
// 求目标串中出现了几个模式串
//====================
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;

struct Trie
{
    int next[500010][26],fail[500010],end[500010];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][buf[i]-'a'] == -1)
                next[now][buf[i]-'a'] = newnode();
            now = next[now][buf[i]-'a'];
        }
        end[now]++;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 26;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 26;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]-'a'];
            int temp = now;
            while( temp != root )
            {
                res += end[temp];
                end[temp] = 0;//如果这里没有清0(删去),那么就会变成重复统计
                temp = fail[temp];
            }
        }
        return res;
    }
    void debug()
    {
        for(int i = 0;i < L;i++)
        {
            printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
            for(int j = 0;j < 26;j++)
                printf("%2d",next[i][j]);
            printf("]\n");
        }
    }
};
char buf[1000010];
Trie ac;
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while( T-- )
    {
        scanf("%d",&n);
        ac.init();
        for(int i = 0;i < n;i++)
        {
            scanf("%s",buf);
            ac.insert(buf);
        }
        ac.build();
        scanf("%s",buf);
        printf("%d\n",ac.query(buf));
    }
    return 0;
}

例题2:HDU5384
给你一堆母串,还有另一堆子串,询问每个母串中出现的子串总次数。(子串计数可重叠)

注:下面这个代码是阉割版的,需要将a变为a[maxn]数组同时遍历每个母串才可以使用。

#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

const int maxn = 1e6 + 5;
int n;
char a[maxn];
char s[maxn];

struct trie {
    int next[maxn][26], fail[maxn], end[maxn];
    int root, cnt;
    int new_node() {
        memset(next[cnt], -1, sizeof next[cnt]);
        end[cnt++] = 0;
        return cnt - 1;
    }
    void init() {
        cnt = 0;
        root = new_node();
    }
    void insert(char *buf) {//字典树插入一个单词
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; i++) {
            int id = buf[i] - 'a';
            if (next[now][id] == -1) {
                next[now][id] = new_node();
            }
            now = next[now][id];
        }
        end[now]++;
    }
    void build() {//构建fail指针
        queue <int> q;
        fail[root] = root;
        for (int i = 0; i < 26; i++) {
            if (next[root][i] == -1) {
                next[root][i] = root;
            }
            else {
                fail[next[root][i]] = root;
                q.push(next[root][i]);
            }
        }
        while (!q.empty()) {
            int now = q.front(); q.pop();
            for (int i = 0; i < 26; i++) {
                if (next[now][i] == -1) {
                    next[now][i] = next[fail[now]][i];
                }
                else {
                    fail[next[now][i]] = next[fail[now]][i];
                    q.push(next[now][i]);
                }
            }
        }
    }
    int query(string buf) {
        int len = buf.length();
        int now = root;
        int res = 0;
        for (int i = 0; i < len; i++) {
            int id = buf[i] - 'a';
            now = next[now][id];
            int tmp = now;
            while (tmp != root) {
                res += end[tmp];
                tmp = fail[tmp];
            }
        }
        return res;
    }
}ac;

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        ac.init();
        scanf("%s", a);
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%s", s);
            ac.insert(s);
        }
        ac.build();
        printf("%d\n", ac.query(a));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38995588/article/details/80659817