AC自动机 模板

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;

typedef long long ll;

const int N = 10010 * 50, M = 1000010;

int tr[N][26], cnt[N], idx;
char str[M];
int q[N], ne[N];

void insert()
{
	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] ++;
}

void build()
{
	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 ++];//i-1
		for(int i = 0; i < 26; i ++)//p[i]
		{
			int c = tr[t][i];//i
			if(!c)continue;
			
			int j = ne[t];
			while(j && !tr[j][i])j = ne[j];
			if(tr[j][i])j = tr[j][i];
			
			ne[c] = j;
			q[++ tt] = c;
		}
	}
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++)
	{
		cin >> str;
		insert();
	}
	
	build();
	
	int ans = 0;
	cin >> str;
	for(int i = 0, j = 0; str[i]; i ++)
	{
		int t = str[i] - 'a';
		while(j && !tr[j][t])j = ne[j];
		if(tr[j][t])j = tr[j][t];
		
		int p = j;
		while(p && cnt[p] != -1)
		{
			ans += cnt[p];
			cnt[p] = -1;
			p = ne[p];
		}
	}
	cout << ans << endl;
}

int main()
{
	IOS
	int _;
	cin >> _;
	while(_ --)
	{
		memset(tr, 0, sizeof tr);
		memset(cnt, 0, sizeof cnt);
		memset(ne, 0, sizeof ne);
		
		solve();
	}
	
	return 0;
}

核心思路是kmp的拓展,只是i++、j++什么的转换成了树的形式,初始化用bfs,每一点的初始化都是借助于该层以前的层进行的。

trie图优化:

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;

typedef long long ll;

const int N = 10010 * 50, M = 1000010;

int tr[N][26], cnt[N], idx;
char str[M];
int q[N], ne[N];

void insert()
{
	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] ++;
}

void build()
{
	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 ++];//i-1
		for(int i = 0; i < 26; i ++)//p[i]
		{
			/*
			int c = tr[t][i];//i
			if(!c)continue;
			
			int j = ne[t];
			while(j && !tr[j][i])j = ne[j];
			if(tr[j][i])j = tr[j][i];
			
			ne[c] = j;
			q[++ tt] = c;
			*/
			int p = tr[t][i];
			if(!p)tr[t][i] = tr[ne[t]][i];
			else
			{
				ne[p] = tr[ne[t]][i];
				q[++ tt] = p;
			}
		}
	}
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++)
	{
		cin >> str;
		insert();
	}
	
	build();
	
	int ans = 0;
	cin >> str;
	for(int i = 0, j = 0; str[i]; i ++)
	{
		int t = str[i] - 'a';
		/*
		while(j && !tr[j][t])j = ne[j];
		if(tr[j][t])j = tr[j][t];
		*/
		j = tr[j][t];
		
		int p = j;
		while(p && cnt[p] != -1)
		{
			ans += cnt[p];
			cnt[p] = -1;
			p = ne[p];
		}
	}
	cout << ans << endl;
}

int main()
{
	IOS
	int _;
	cin >> _;
	while(_ --)
	{
		memset(tr, 0, sizeof tr);
		memset(cnt, 0, sizeof cnt);
		memset(ne, 0, sizeof ne);
		
		solve();
	}
	
	return 0;
}

ne[t]是回溯一次,tr[ne[t]][i]直接记录好了它下一个点的位置,存在儿子就到儿子,没有儿子就是记录的回溯好的点。

每个点的ne都被计算了。

纯板子:

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;

typedef long long ll;

const int N = 10010 * 50, M = 1000010;

int tr[N][26], cnt[N], idx;
char str[M];
int q[N], ne[N];

void insert()
{
	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] ++;
}

void build()
{
	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 ++];//i-1
		for(int i = 0; i < 26; i ++)//p[i]
		{
			int c = tr[t][i];//i
			if(!c)continue;
			
			int j = ne[t];
			while(j && !tr[j][i])j = ne[j];
			if(tr[j][i])j = tr[j][i];
			
			ne[c] = j;
			q[++ tt] = c;
		}
	}
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++)
	{
		cin >> str;
		insert();
	}
	
	build();
	
	int ans = 0;
	cin >> str;
	for(int i = 0, j = 0; str[i]; i ++)
	{
		int t = str[i] - 'a';
		while(j && !tr[j][t])j = ne[j];
		if(tr[j][t])j = tr[j][t];
		
		int p = j;
		while(p && cnt[p] != -1)
		{
			ans += cnt[p];
			cnt[p] = -1;
			p = ne[p];
		}
	}
	cout << ans << endl;
}

int main()
{
	IOS
	int _ = 1;
	//cin >> _;
	while(_ --)
	{
		//memset(tr, 0, sizeof tr);
		//memset(cnt, 0, sizeof cnt);
		//memset(ne, 0, sizeof ne);
		
		solve();
	}
	
	return 0;
}
相关推荐
我在人间贩卖青春5 小时前
C++之多重继承
c++·多重继承
颜酱5 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919105 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878385 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
DuHz6 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
Polaris北极星少女6 小时前
TRSV优化2
算法
代码游侠7 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
2301_763472467 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
阿猿收手吧!7 小时前
【C++】std::promise原理与实战解析
c++
abluckyboy8 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法