洛谷-P5357-【模板】AC自动机(二次加强版)
题目传送门
-------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题
sol:AC自动机,还是要解决跳fail边产生的重复访问,但是这次用last边已经不行了,只能拿76分。我们把跳fail边的过程放到串扫描完之后一次性进行。
- AC自动机
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MAXN = ;
struct Trie {
int son[MAXN][], fail[MAXN];
int que[MAXN]; int head, tail;
int cnt[MAXN]; int tot, root;
int add_node() {
memset(son[tot], -, sizeof(son[tot]));
cnt[tot] = ;
return tot ++;
}
void init() {
head = tail = ;
tot = ;
root = add_node();
}
int insert(char* s) {
int p = root;
for (int i = ; s[i]; i++) {
int index = s[i] - 'a';
if (son[p][index] == -)
son[p][index] = add_node();
p = son[p][index];
}
return p;
}
void build() {
fail[root] = root;
for (int i = ; i < ; i++) {
if (son[root][i] == -) son[root][i] = root;
else {
fail[son[root][i]] = root;
que[++ tail] = son[root][i];
}
}
while (head != tail) {
int p = que[++ head];
for (int i = ; i < ; i++) {
if (son[p][i] == -) son[p][i] = son[fail[p]][i];
else {
fail[son[p][i]] = son[fail[p]][i];
que[++ tail] = son[p][i];
}
}
}
}
void slove(char* s) {
int p = root;
for (int i = ; s[i]; i++) {
int index = s[i] - 'a';
p = son[p][index];
cnt[p] ++;
}
for (int i = tail; i >= ; i--) {
int p = que[i];
cnt[fail[p]] += cnt[p];
}
}
} ac;
int inde[MAXN]; // 本地运行没什么问题,洛谷叫index就报编译错误
char s[];
int main() {
int n; ac.init();
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%s", s);
inde[i] = ac.insert(s);
}
ac.build();
scanf("%s", s); ac.slove(s);
for (int i = ; i <= n; i++)
printf("%d\n", ac.cnt[inde[i]]);
return ;
}经过不断地40分、60分、76分,终于搞定了。为了在最后跳fail边的时候保证深度越高的节点越先跳采用了手写队列保留bfs时候的层次关系。网上看到别人的代码据说是用拓扑的,没细看。不过感觉这里的队列就是一个对深度的拓扑排序了。
洛谷-P5357-【模板】AC自动机(二次加强版)的更多相关文章
- 洛谷P3808 & P3796 AC自动机模板
题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...
- 洛谷 - P3966 - 单词 - AC自动机
https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次 ...
- 洛谷.3121.审查(AC自动机 链表)
题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...
- 洛谷 - P2444 - 病毒 - AC自动机
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...
- 洛谷 P3804 [模板] 后缀自动机
题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
- 【洛谷 P5357】 【模板】AC自动机(二次加强版)(AC自动机,差分)
每次匹配都不停跳fail显然太慢了,于是在每个节点和fail指向的点连一条边,构成一棵树,在这棵树上差分一下就好了. AC自动机 就这个算法而言其实没用想象中那么难. #include <cst ...
- 【AC自动机】洛谷三道模板题
[题目链接] https://www.luogu.org/problem/P3808 [题意] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. [题解] 不再介绍基础知识了,就是裸的模 ...
- 洛谷-P3796-【模板】AC自动机(加强版)
题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,在fail边的基础上再加一个last边, ...
随机推荐
- i春秋-web-爆破3
首先,是PHP代码审计,看懂就能解出来题. <?php error_reporting(0); session_start(); require('./flag.php'); if(!isset ...
- <kotlin>基础,杂七杂八(亲测有效)
okhttp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) ...
- SQL基础之JDBC、连接池
JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...
- POJ 1562:Oil Deposits
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14462 Accepted: 7875 Des ...
- C++ 一般模板友元关系
//一般模板友元关系 #include "stdafx.h" #include <iostream> using namespace std; template< ...
- Tensorflow学习教程------非线性回归
自己搭建神经网络求解非线性回归系数 代码 #coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pypl ...
- 18 12 30 服务器 Django 的初步使用 环境变量的调整
1.安装django 1.1.下载Django包 https://www.djangoproject.com/download/https://www.djangoproject.com/m/rele ...
- php 常用编译参数
安装依赖 yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng li ...
- Cracking Digital VLSI Verification Interview 第一章
目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...
- [极客大挑战 2019]Http
0x00知识点 了解HTTP协议,使用bp伪造. 0x01 解题 首先查看源代码,找到Secret.php 访问 使用bp查看 提示我们需要来自该网址,直接改header头信息即可,我们可以通过使用r ...