模板——AC自动机

#include<bits/stdc++.h>
using namespace std;
struct nob{
	int fail,son[27],ed;
}a[1000000];
int cnt=0;
void build (string s){
	int now=0;
	for (int i=0; i<s.length(); i++){
		if (a[now].son[s[i]-‘a‘]==0)
			a[now].son[s[i]-‘a‘]=++cnt;
		now=a[now].son[s[i]-‘a‘];
	}
	a[now].ed++;
}
void fail(){
	queueque;
	for (int i=0; i<26; i++){
		if (a[0].son[i]!=0){
			a[a[0].son[i]].fail=0;
			que.push(a[0].son[i]);
		}
	}
	while (que.size()){
		int pos=que.front();
		que.pop();
		for (int i=0; i<26; i++){
			if (a[pos].son[i]!=0){
				a[a[pos].son[i]].fail=a[a[pos].fail].son[i];
				que.push(a[pos].son[i]);
			}
			else a[pos].son[i]=a[a[pos].fail].son[i];
		}
	}
}
int answer(string s){
	int now=0,ans=0;
	for (int i=0; i<s.length(); i++){
		now=a[now].son[s[i]-‘a‘];
		for (int t=now; t&&a[t].ed!=-1; t=a[t].fail){
			ans+=a[t].ed;
			a[t].ed=-1;
		}
	}
	return ans;
}
int main(){
	int n;
	string s;
	cin>>n;
	for (int i=1; i<=n; i++){
		cin>>s;
		build(s);
	}
	a[0].fail=0;
	fail();
	cin>>s;
	cout<<answer(s)<<endl;
	return 0;
}
时间: 2024-12-11 01:08:18

模板——AC自动机的相关文章

算法模板——AC自动机

实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有时候字典会相当稀疏,所以引入了chi和bro指针进行优化——其原理比较类似于邻接表,这个东西本身和next数组本质上是一致的,只是chi和bro用于遍历某一节点下的子节点,next用于查询某节点下是否有需要的子节点) 1 type 2 point=^node; 3 node=record 4 ex:

[模板]AC自动机(1)

题目描述 给定一个文本串和多个模式串,求有几个模式串出现在文本串中 #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 1000005 char s[MAXN]; int N; struct queue{ int que[MAXN];int head,tail; queue():head(1),tail(0){} inline void pop(){head++;} inline

AC自动机例题

P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using namespace std; typedef long long LL; const int INF=1e9+7; inline LL read(){ register LL x=0,f=1;register char c=getchar(); while(c<48||c>57){if(c=='-')f=

ac自动机基础模板(hdu2222)

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, th

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

hdu2222(ac自动机模板)

先推荐两篇写的很好的ac自动机blog: http://blog.csdn.net/creatorx/article/details/71100840 http://blog.csdn.net/niushuai666/article/details/7002823 正题 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意: 给出 n 个模式串以及一个 主串, 问有多少个模式串在主串中出现过 思路: ac自动机模板题 代码: 1 #inc

Match:Keywords Search(AC自动机模板)(HDU 2222)

多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #define MAX 26 6 7 using namespace std; 8 9 struct node 10 {

[hdu2222]ac自动机(模板)

题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map&g