ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

louguP3966 [TJOI2013]单词【AC自动机】

2022-08-21 09:06:30  阅读:133  来源: 互联网

标签:index AC ch int louguP3966 tot TJOI2013 fail


小时候一直不理解为什么老人会呆呆地坐着,望着远方很久很久

少年不会知道自己的勇气意味着什么,他只是在武汉四十度的天气下奋力奔跑。在军训伊始终于成功联系上了导师,一个小时内赶出简历,基于事实发展创造:),既对自身能力惶惶,又隐隐有些期待。我从没想过自己连复读的经历都能拿来利用,当我写上“21年河南大学学前教育专业,复读”时自己都感觉想笑,必要时,人是会连伤疤都当作资本的存在,四食堂的蒸蛋与人心都是琢磨不透的东西。想要得到「我没有逃避」的记忆而进行多串匹配,想到\(AC\)自动机,但是原本的\(AC\)自动机是不能处理相同串的,所以,需要用一个\(tot\)数组统计数量,再跳\(fail\)得到答案,注意手写队列保证有序性

$click$ $for$ $codes$
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e6 + 3;
struct AC {
	int ch[N][26], fail[N], tot[N], ed[N];
	int id_index = 0, trie_index = 0;
	void insert(char *str) { // build a trie
		int len = strlen(str + 1), u = 0;
		for(int i = 1; i <= len; ++i) {
			int v = str[i] - 'a'; // str[i] ^ 'a' is wrong here
			if(!ch[u][v]) ch[u][v] = ++trie_index;
			u = ch[u][v];
			++tot[u];
		}
		ed[++id_index] = u; // note the node corresponding to the end position of each string
	}
	void build() { // build the Aho-Corasick automation
		int q[N], h = 0, t = 0; // here, we wirite the queue ourselves, so that the incoming nodes can be arranged according to the queue order
		for(int i = 0; i < 26; ++i) { // start BFS from root 0
			if(ch[0][i]) {
				q[++t] = ch[0][i];
			}
		}
		while(h < t) {
			int u = q[++h];
			for(int i = 0; i < 26; ++i) {
				if(ch[u][i]) { // if child nodes are included
					fail[ch[u][i]] = ch[fail[u]][i]; // set fail: node(child) -> node(fail: parient -> same value)
					q[++t] = ch[u][i]; // come with me for a little ride, see the shadows passing by
				}
				else {
					ch[u][i] = ch[fail[u]][i]; // there is no reason to stay, goodbye mama, I'm going sailing tonight
				}
			}
		}
		for(int i = trie_index; i; --i) { // transfer the information of child nodes upward, and note that the use of queue ensures the order
			tot[fail[q[i]]] += tot[q[i]];
		}
	}
} AC;
char str[N];
int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%s", str + 1);
		AC.insert(str);
	}
	AC.build();
	for(int i = 1; i <= n; ++i)
		printf("%d\n", AC.tot[AC.ed[i]]);
	return 0;
}

image

标签:index,AC,ch,int,louguP3966,tot,TJOI2013,fail
来源: https://www.cnblogs.com/bingoyes/p/16609289.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有