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];
            
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是AC自动机Java模板题U301874的代码实现: ```java import java.io.*; import java.util.*; public class Main { static final int MAXN = 100010, MAXM = 200010; static final int INF = 0x3f3f3f3f; static int n, m, cnt; static int[] trie = new int[MAXN * 30], idx = new int[MAXN * 30]; static int[] fail = new int[MAXN * 30], vis = new int[MAXN * 30]; static int[] head = new int[MAXN], nxt = new int[MAXM], ver = new int[MAXM], tot; static int[] deg = new int[MAXN]; static char[][] str = new char[MAXN][30]; static Map<Character, Integer> map = new HashMap<>(); static int add(char[] s) { int p = 0; for (int i = 0; s[i] != '\0'; i++) { char c = s[i]; if (!map.containsKey(c)) { map.put(c, ++cnt); } int u = map.get(c); if (trie[p] == 0) { trie[p] = ++tot; } p = trie[p]; idx[p] = u; } return p; } static void build() { Queue<Integer> q = new LinkedList<>(); for (int i = 1; i <= cnt; i++) { int u = map.get(str[i][0]); if (trie[0] == 0) { trie[0] = ++tot; } int p = trie[0]; idx[p] = 0; if (trie[p + u] == 0) { trie[p + u] = ++tot; } fail[p + u] = p; q.offer(p + u); } while (!q.isEmpty()) { int u = q.poll(); for (int i = head[idx[u]]; i != 0; i = nxt[i]) { int v = ver[i]; int p = fail[u], q = 0; while (p != 0 && trie[p + v] == 0) { p = fail[p]; } if (trie[p + v] != 0) { q = trie[p + v]; } fail[u + v] = q; q.offer(u + v); } } } static void addEdge(int u, int v) { ver[++tot] = v; nxt[tot] = head[u]; head[u] = tot; } static void topo() { Queue<Integer> q = new LinkedList<>(); for (int i = 1; i <= tot; i++) { if (deg[i] == 0) { q.offer(i); } } while (!q.isEmpty()) { int u = q.poll(); vis[u] = 1; for (int i = head[u]; i != 0; i = nxt[i]) { int v = ver[i]; deg[v]--; if (deg[v] == 0) { q.offer(v); } } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); for (int i = 1; i <= n; i++) { String s = in.next(); str[i] = s.toCharArray(); add(str[i]); } m = in.nextInt(); for (int i = 1; i <= m; i++) { String s = in.next(); int len = s.length(); int p = 0; for (int j = 0; j < len; j++) { char c = s.charAt(j); if (!map.containsKey(c)) { break; } int u = map.get(c); if (trie[p + u] == 0) { break; } p = trie[p + u]; deg[p]++; addEdge(p, p + u); } } build(); topo(); for (int i = 1; i <= n; i++) { int p = 0; for (int j = 0; str[i][j] != '\0'; j++) { p = trie[p + map.get(str[i][j])]; if (vis[p] == 1) { System.out.println("YES"); break; } } if (vis[p] == 0) { System.out.println("NO"); } } } } ``` 该题解释:给定 $n$ 个模式串和 $m$ 个文本串,问每个模式串是否存在于文本串中。其中,模式串和文本串都只包含小写字母。 AC自动机是一种可以高效匹配多个模式串的数据结构。该题需要使用AC自动机进行多模式串匹配。 代码实现中,使用一个trie树存储所有模式串,每个节点记录了下一层的字符和对应的子节点编号。同时,使用一个map记录每个字符对应的编号,以便于在trie树中查找。 在trie树构建完成后,使用广度优先搜索构建fail指针。搜索过程中,对于每个节点 $u$,依次查找其父亲节点 $p$ 直到根节点,若 $p$ 的子节点 $v$ 与 $u$ 的子节点 $w$ 匹配,则令 $u$ 的fail指针指向 $p+v$ 节点。如果 $p+v$ 节点不存在,则继续向根节点搜索。 在fail指针构建完成后,对于每个文本串,从根节点开始依次匹配每个字符,直到匹配完成或者无法匹配。如果最终匹配的节点已经被访问,则说明该模式串存在于文本串中。 时间复杂度为 $O(\sum |P|+|T|)$,其中 $\sum |P|$ 表示所有模式串的长度之和,$|T|$ 表示所有文本串的长度之和。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值