AC自动机

AC自动机

感觉非常合理,但是又不会证明,就先这样吧
感觉还有至少三个问题:
1、为什么要在空的子结点上连自己的fail结点的对应子结点(注释1)可以把整个自动机变成一个\(DAG\)
2、为什么u结点不需要参与转移(注释2)同上
3、为什么遇到end == -1就可以break(注释3)这题只需要统计是否出现过不用统计次数

昨天又做了几题,差不多会用板子了(只能意会

洛谷P3808【模板】AC 自动机(简单版)

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 1e6 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int n;
string s, str;

struct Trie {
	int son[26];
	int end;
	int fail;
} tr[N];

int idx;

void insert(string &a) {
	int u = 0;
	rep (i, 0, a.length() - 1) {
		int v = a[i] - 'a';
		if (tr[u].son[v]) u = tr[u].son[v];
		else tr[u].son[v] = ++idx, u = idx;
	}
	tr[u].end++;
}

void build() {
	queue<int> Q;
	rep (i, 0, 25) if (tr[0].son[i]) Q.push(tr[0].son[i]);

	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		rep (i, 0, 25) {
			int v = tr[u].son[i];
			if (!v) tr[u].son[i] = tr[tr[u].fail].son[i];
			else tr[v].fail = tr[tr[u].fail].son[i], Q.push(v); // 1
		}
	}
}

void work() {
	cin >> n;
	rep (i, 1, n) {
		cin >> s;
		insert(s);
	}

	build();

	cin >> str;

	int u = 0, ans = 0;
	rep (i, 0, str.length() - 1) {
		u = tr[u].son[str[i] - 'a']; // 2
		for (int t = u; t && tr[t].end != -1; t = tr[t].fail) { // 3
			ans += tr[t].end;
			tr[t].end = -1;
		}
	}
	
	cout << ans << endl;
}

signed main() {
	IO

	int test = 1;
	//	cin >> test;

	while (test--) {
		work();
	}

	return 0;
}

Acwing 1282. 搜索关键词

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 1e4 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int n;
string s, str;

struct Trie {
	int son[26];
	int end;
	int fail;
} tr[N * 26];

int idx;

void insert(string &a) {
	int u = 0;
	rep (i, 0, a.length() - 1) {
		int v = a[i] - 'a';
		if (tr[u].son[v]) u = tr[u].son[v];
		else tr[u].son[v] = ++idx, u = idx;
	}
	tr[u].end++;
}

void build() {
	queue<int> Q;
	rep (i, 0, 25) if (tr[0].son[i]) Q.push(tr[0].son[i]);

	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		rep (i, 0, 25) {
			int v = tr[u].son[i];
			if (!v) tr[u].son[i] = tr[tr[u].fail].son[i];
			else tr[v].fail = tr[tr[u].fail].son[i], Q.push(v);
		}
	}
}

void work() {
    idx = 0;
    memset(tr, 0, sizeof tr);
    
	cin >> n;
	rep (i, 1, n) {
		cin >> s;
		insert(s);
	}

	build();

	cin >> str;

	int u = 0, ans = 0;
	rep (i, 0, str.length() - 1) {
		u = tr[u].son[str[i] - 'a'];
		for (int t = u; t && tr[t].end != -1; t = tr[t].fail) {
			ans += tr[t].end;
			tr[t].end = -1;
		}
	}
	
	cout << ans << endl;
}

signed main() {
	IO

	int test = 1;
	cin >> test;

	while (test--) {
		work();
	}

	return 0;
}

P3796 【模板】AC 自动机(加强版)
前面三题都差不多

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 2e5 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int n;
int id[210];

string s, str;

vector<int> v;

struct Trie {
	int son[26];
	int end;
	int fail;
	int res;
	string t;
} tr[N];

int idx;

int insert(string &a) {
	int u = 0;
	rep (i, 0, a.length() - 1) {
		int v = a[i] - 'a';
		if (tr[u].son[v]) u = tr[u].son[v];
		else tr[u].son[v] = ++idx, u = idx;
	}
	tr[u].end++;
	tr[u].t = a;
	return u;
}

void build() {
	queue<int> Q;
	rep (i, 0, 25) if (tr[0].son[i]) Q.push(tr[0].son[i]);
	
	while (!Q.empty()) {
		int u = Q.front();
		v.push_back(u);
		Q.pop();
		rep (i, 0, 25) {
			int v = tr[u].son[i];
			if (!v) tr[u].son[i] = tr[tr[u].fail].son[i];
			else tr[v].fail = tr[tr[u].fail].son[i], Q.push(v);
		}
	}
}

void work() {
	while (cin >> n, n) {
		idx = 0;
		v.clear();
		memset(tr, 0, sizeof tr);
		
		rep (i, 1, n) {
			cin >> s;
			id[i] = insert(s);
		}
		
		build();
		
		cin >> str;
		
		int u = 0;
		rep (i, 0, str.length() - 1) {
			u = tr[u].son[str[i] - 'a'];
			for (int t = u; t; t = tr[t].fail) if(tr[t].end) tr[t].res++;
		}
		
		int mx = 0;
		vector<string> ans;
		
		rep (i, 1, idx) {
			if (tr[i].res > mx) ans.clear(), ans.push_back(tr[i].t), mx = tr[i].res;
			else if (tr[i].res == mx) ans.push_back(tr[i].t);
		}
		
		cout << mx << endl;
		rep (i, 0, ans.size() - 1) cout << ans[i] << endl;
	}
}

signed main() {
	IO
	
	int test = 1;
	//	cin >> test;
	
	while (test--) {
		work();
	}
	
	return 0;
}

Acwing 1285. 单词
直接统计最坏\(O(n^2)\),我们发现在自动机的trie图上每个结点的出度有且只有一个,并且指向的是拥有相同后缀的单词,所以我们可以在统计完每个结点的单点贡献后,利用拓扑序进行累加,复杂度\(O(n)\)

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 1e6 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int n;
int id[210], f[N];

string s, str;

vector<int> v;

struct Trie {
	int son[26];
	int end;
	int fail;
} tr[N];

int idx;

int insert(string &a) {
	int u = 0;
	rep (i, 0, a.length() - 1) {
		int v = a[i] - 'a';
		if (tr[u].son[v]) u = tr[u].son[v];
		else tr[u].son[v] = ++idx, u = idx;
		f[u]++;
	}
	tr[u].end++;
	return u;
}

void build() {
	queue<int> Q;
	rep (i, 0, 25) if (tr[0].son[i]) Q.push(tr[0].son[i]);
	
	while (!Q.empty()) {
		int u = Q.front();
		v.push_back(u);
		Q.pop();
		rep (i, 0, 25) {
			int v = tr[u].son[i];
			if (!v) tr[u].son[i] = tr[tr[u].fail].son[i];
			else tr[v].fail = tr[tr[u].fail].son[i], Q.push(v);
		}
	}
}

void work() {
	cin >> n;
	rep (i, 1, n) {
		cin >> s;
		id[i] = insert(s);
	}
	
	build();
	
	rrep (i, idx - 1, 0) f[tr[v[i]].fail] += f[v[i]];
	
	rep (i, 1, n) cout << f[id[i]] << endl;
}

signed main() {
	IO
	
	int test = 1;
	//	cin >> test;
	
	while (test--) {
		work();
	}
	
	return 0;
}

P5357 【模板】AC 自动机(二次加强版)
跟上一题类似,先遍历统计原串的单点贡献,再按照拓扑序累加

#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 2e5 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int n;
int f[N], id[N];

string s, str;

struct Trie {
	int son[26];
	int end;
	int fail;
	int res;
} tr[N];

int idx;

vector<int> vc;

int insert(string &a) {
	int u = 0;
	rep (i, 0, a.length() - 1) {
		int v = a[i] - 'a';
		if (tr[u].son[v]) u = tr[u].son[v];
		else tr[u].son[v] = ++idx, u = idx;
	}
	tr[u].end++;
	return u;
}

void build() {
	queue<int> Q;
	rep (i, 0, 25) if (tr[0].son[i]) Q.push(tr[0].son[i]);
	
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		vc.push_back(u);
		rep (i, 0, 25) {
			int v = tr[u].son[i];
			if (!v) tr[u].son[i] = tr[tr[u].fail].son[i];
			else tr[v].fail = tr[tr[u].fail].son[i], Q.push(v);
		}
	}
}

void work() {
	cin >> n;
	
	rep (i, 1, n) {
		cin >> s;
		id[i] = insert(s);
	}
	
	build();
	
	cin >> str;
	
	int u = 0;
	rep (i, 0, str.length() - 1) {
		u = tr[u].son[str[i] - 'a'];
		f[u]++;
	}
	
	rrep (i, idx - 1, 0) f[tr[vc[i]].fail] += f[vc[i]];
	
	rep (i, 1, n) cout << f[id[i]] << endl;
}

signed main() {
	IO
	
	int test = 1;
	//	cin >> test;
	
	while (test--) {
		work();
	}
	
	return 0;
}
posted @ 2022-08-12 23:29  xhy666  阅读(51)  评论(0)    收藏  举报