AC自动机(Automaton)

小小总结

  1. 千万记住insert完以后要build!!!老是忘掉QAQ
  2. insert函数就只是trie数的插入函数,可在结尾根据条件打一些标记
  3. build函数(构建fail树),主要利用BFS
  4. match函数中跑fail树其实就是反复跑后缀
  5. fail树上倒过来建树以后可以进行很多高端操作,比如阿狸的打字机(也许还能树剖一下QAQ)
  6. match函数中跑fail树过程可以转化为打标记,而避免重复跑一个相同的后缀,常见优化(见下面的二次加强版板子题)
  7. AC自动机常与数位DP结合,毕竟数字和字符串长得差不多

洛谷-P3808 简单板子题

//#pragma comment(linker, "/STACK:102400000,102400000")
#include "bits/stdc++.h"
#define pb push_back
#define ls l,m,now<<1
#define rs m+1,r,now<<1|1
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 1e5+10;
const int mod = 1e9+7;
const double eps = 1e-9;

int trie[maxn][26], cnt;
int e[maxn];
int fail[maxn];

void insert(char *s) {
    int p=0;
    for(int i=0; s[i]; ++i) {
        int k=s[i]-'a';
        if(!trie[p][k]) trie[p][k]=++cnt;
        p=trie[p][k];
    }
    e[p]++;
}

void 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值