
AC自动机
���������������������
发布日期:2021-05-14 16:54:59
浏览次数:14
分类:精选文章
本文共 4150 字,大约阅读时间需要 13 分钟。
������ n ������������������ 50 ������������������������������������������������������������ m ���������������������������������������������������������������������������
1. ���������������
������������������������������������������������������������������������������������������������������������������������������������ (BFS) ������������������������������������������
2. ������������������������������
- ��������� (Prefix Tree) ��� tries������������������������������������������������������������������������������������������������������������������������
- ������������������ (BFS)������������������������������������������������������������������������
3. ���������������������
- ������������������������������
tr
���������������������tr[p][i]
���������p
���������������i
������������������������������������������������������ 0��� idx
������������������������������������������
- ���������������
- ������������������������������������������������������������������������
c
���������������������������������������������������������������������������������������������������������������������������������idx
��������������������������������� - ������������������������������
cnt[p]
������������������������������������������ - ���������������������������
- ������������������������������������������������������������
- ������������ BFS ���������������������������������������������������������������������������������������������������������������
ne
������������������������������������������ - ���������������������������������������������������������������������������
- ���������������������
- ������������������������������������������������������������������������������
- ������������������������������������������������������������������������������������������������������
- ���������������
str
������������������������������ - ������������������
tr[p][t]
���������p
���������������t
��������������� - ���������
q
������������������������������������ - ������������
cnt
������������������������������������ - ���������������
ne
������������������������������������������������ - ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
- ���������������������������������������������������������
cnt[p]
��� - ���������������������������������������������������������������������������
- ���������������������������������������������������������������������������������������
ne
��������������������������������� - ������������������������������������������������
add()
������������������������ - ������������������������������������������������
4. ���������������������
#includeusing namespace std;const int N = 10010;const int S = 55;const int M = 1000010;char str[M];int tr[N * S][26];int q[N * S];int idx;int cnt[N * S];int ne[N * S];
5. ���������������
void add() { int p = 0; for (int i = 0; str[i]; i++) { int t = str[i] - 'a'; if (!tr[p][t]) { tr[p][t] = ++idx; } p = tr[p][t]; } cnt[p]++;}
6. ���������������������
void bfs() { int hh = 0, tt = -1; for (int i = 0; i < 26; i++) { if (tr[0][i]) { q[++tt] = tr[0][i]; } } while (hh <= tt) { int t = q[hh++]; for (int i = 0; i < 26; i++) { int j = ne[t]; int c = tr[t][i]; if (!c) continue; while (j && !tr[j][i]) j = ne[j]; if (tr[j][i]) j = tr[j][i]; ne[c] = j; q[++tt] = c; } }}
7. ������������������
int main() { int t; cin >> t; while (t--) { memset(cnt, 0, sizeof(cnt)); memset(ne, 0, sizeof(ne)); memset(tr, 0, sizeof(tr)); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> str; add(); } for (int i = 0; i < M; i++) { ... // ������������������ } }}
8. ���������������
��������������������������������� BFS ������������������������������������������������������������������������������������
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月24日 07时14分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用python绘出常见函数
2021-05-15
Golang AES加密
2021-05-15
Puppet的一些奇技淫巧
2021-05-15
foreman源NO_PUBKEY 6F8600B9563278F6
2021-05-15
亚马逊aws文档语法错误
2021-05-15
什么是5G?居然有人用漫画把它讲得如此接地气!
2021-05-15
Spring cloud --分布式配置中心组件Spring Cloud Config
2021-05-15
UE4接入Android第三方库2——通过JIN与GameActivity通信
2021-05-15
Unity Job System 2——并行处理数据
2021-05-15
BIG解决保险欺诈问题,开创数字化保险时代
2021-05-15
Apache JMeter5.3 压力测试
2021-05-15
c++ hpp使用好处
2021-05-15
Mac 使用Eclipse老是闪退解决方案
2021-05-15
谈笑间学会-Hbase Rowkey设计
2021-05-15
spark概述
2021-05-15
[密码学] RSA同模攻击与选择密文攻击
2021-05-15
JavaScript 知识梳理[一] 变量类型,浅拷贝,深拷贝
2021-05-15
Linux学习笔记(二):文件权限与目录配置
2021-05-15
Coursera普林斯顿算法课第二次作业
2021-05-15
pip命令 failed to create process.
2021-05-15