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;
}
相关推荐
Niuguangshuo5 分钟前
音频特征提取算法介绍
算法·音视频
Vect__10 分钟前
从底层到上层的“外挂”:deque、stack、queue、priority_queue 全面拆解
数据结构·c++
草莓熊Lotso13 分钟前
C++ 手写 List 容器实战:从双向链表原理到完整功能落地,附源码与测试验证
开发语言·c++·链表·list
wxin_VXbishe16 分钟前
基于SpringBoot的天天商城管理系统的设计与现-计算机毕业设计源码79506
java·c++·spring boot·python·spring·django·php
fengfuyao98520 分钟前
基于MATLAB的匈牙利算法实现任务分配
算法·数学建模·matlab
初圣魔门首席弟子25 分钟前
const string getWord() ;和 string getWord() const ;是一样的效果吗
开发语言·c++
CoovallyAIHub25 分钟前
超详细链式插补 (MICE) 多元插补:机器学习模型的高级缺失数据处理
算法·机器学习·计算机视觉
明天会有多晴朗43 分钟前
C语言入门教程(第6讲):函数——让程序学会“分工合作”的魔法
c语言·开发语言·算法
泽虞1 小时前
《Qt应用开发》笔记p3
linux·开发语言·数据库·c++·笔记·qt·面试
ajassi20001 小时前
开源 C++ QT QML 开发(十八)多媒体--音频播放
c++·qt·开源