如形の博客

洛谷炸了

洛谷炸了

洛谷炸了

模板代码(P3808 AC 自动机(简单版)

#include<bits/stdc++.h>
using namespace std;
int n,tot,cnt[1000005],ch[1000005][30],ne[1000005];
void insert(string s){
	int u=0;
	for(char c:s){
		int v=c-'a';
		if(!ch[u][v])ch[u][v]=++tot;
		u=ch[u][v];
	}
	cnt[u]++;
}
void build(){
	queue<int>q;
	for(int i=0;i<26;++i){
		if(ch[0][i])q.push(ch[0][i]);
	}
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=0;i<26;++i){
			int v=ch[u][i];
			if(v)ne[v]=ch[ne[u]][i],q.push(v);
			else ch[u][i]=ch[ne[u]][i];
		}
	}
}
int query(string s){
	int ans=0;
	for(int k=0,i=0;s[k];k++){
		i=ch[i][s[k]-'a'];
		for(int j=i;j&&~cnt[j];j=ne[j]){
			ans+=cnt[j],cnt[j]=-1;
		}
	}
	return ans;
}
string t;
int main(){
	cin>>n;
	for(int i=1;i<=n;++i){
		string s;
		cin>>s;
		insert(s);
	}
	cin>>t;
	build();
	cout<<query(t);
	return 0;
}
cpp
AC 自动机笔记
https://example.github.io/article/automated-ac
Author 如形
Published at June 6, 2026
Comment seems to stuck. Try to refresh?✨