AC自动机板子(from. qwer)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int N=;
struct Tri
{
int Next[N][], fail[N], val[N], root, L;
void init() {L = ;root = newnode();}
int newnode()
{
for(int i = ;i < ;i++) Next[L][i] = -;
val[L++] = ;
return L - ;
} void insert(char s[])
{
int len = strlen(s), cur = root;
for(int i = ;i < len;i++)
{
if(Next[cur][s[i]-'a'] == -)
Next[cur][s[i]-'a'] = newnode();
cur = Next[cur][s[i]-'a'];
}
val[cur]++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;i++)
if(Next[root][i] == -) Next[root][i] = root;
else
{
fail[Next[root][i]] = root;
Q.push(Next[root][i]);
}
while(!Q.empty())
{
int cur = Q.front(); Q.pop();
for(int i = ;i < ;i++)
if(Next[cur][i] == -)
Next[cur][i] = Next[fail[cur]][i];
else
{
fail[Next[cur][i]]=Next[fail[cur]][i];
Q.push(Next[cur][i]);
}
}
}
int query(char s[])
{
int len = strlen(s), cur = root, res = ;
for(int i = ;i < len;i++)
{
cur = Next[cur][s[i]-'a'];
int tmp = cur;
while (tmp != root)
{
res += val[tmp];
val[tmp] = ;
tmp = fail[tmp];
}
}
return res;
}
};
char s[N];
Tri AC;
int n;
int main()
{
scanf("%d",&n);
AC.init();
for(int i = ;i < n;i++)
{
scanf("%s",s);
AC.insert(s);
}
AC.build();
scanf("%s",s);
printf("%d\n",AC.query(s));
return ;
}
AC自动机板子(from. qwer)的更多相关文章
- AC自动机板子题/AC自动机学习笔记!
想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...
- hdu_2222_Keywords Search(AC自动机板子)
题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- ac自动机板子
hdu2222 #include<bits/stdc++.h> #define ll long long #define M 500005 using namespace std; int ...
- 模板 AC自动机
题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...
- 洛谷P3966 单词 [TJOI2013] AC自动机
正解:AC自动机 解题报告: 传送门! 先来提供一个40pts错解QAQ 首先看到这题就会想到AC自动机板子题2鸭!然后就照着那题的套路打一下,随便改一点儿,简单来说就是每次经过一个节点都要++,然后 ...
- P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
随机推荐
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Maximum Subsequence Sum 【DP】
Given a sequence of K integers { N1, N2, -, NK }. A continuous subsequence is defined to be ...
- java多线程系列 JUC锁01 框架
转载 http://www.cnblogs.com/skywang12345/p/3496098.html 参考 https://www.cnblogs.com/leesf456/p/5453091. ...
- Appium——元素定位
首先介绍两种定位元素的工具,appium自带的 Inspector 和 android SDK自带的 uiautomatorviewer 1.UIAutomator Viewer比较简单,在模拟器打开 ...
- Spring Boot2.0之@Async实现异步调用
补充一个知识点: lombok底层原理使用的是: 字节码技术ASM修改字节码文件,生成比如类似于get() set( )方法 一定要在开发工具安装 在编译时候修改字节码文件(底层使用字节码技术),线上 ...
- matlab之结构体数组struct
以下内容来自于:https://blog.csdn.net/u010999396/article/details/54413615/ 要在MALTAB中实现比较复杂的编程,就不能不用struct类型. ...
- 获取url的参数
function getPrams () { var url, urlPrams, urlPramsArr=[], item=[], prams={}; url = location.href; if ...
- ES6 generator 基础
参考文档 harmony:generators Generator是ES6的新特性,通过yield关键字,可以让函数的执行流挂起,那么便为改变执行流程提供了可能. 创建Generator functi ...
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Python实现排序算法之快速排序
Python实现排序算法:快速排序.冒泡排序.插入排序.选择排序.堆排序.归并排序和希尔排序 Python实现快速排序 原理 首先选取任意一个数据(通常选取数组的第一个数)作为关键数据,然后将所有比它 ...