首页 > 其他分享 >louguP3966 [TJOI2013]单词【AC自动机】

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

时间:2022-08-21 08:45:09浏览次数:46  
标签: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

标签:AC,ch,int,louguP3966,tot,TJOI2013,fail,自动机
From: https://www.cnblogs.com/bingoyes/p/16609289.html

相关文章

  • 2022年8月21日周六总结(maven install和package的区别未完成)
    最近做了nexus的配置,突然发现maven也很重要,我们平时会在idea用到clear、install、package等,package毫无疑问就是打包jar包了(在maven中定义了),这个打包会把 最近:这里记录......
  • Mac目录下打开iterm2终端
    iterm2使用教程:http://yu66.vip/doc/mac/005-Mac%E4%B8%8BIterm2%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%BF%AB%E6%8D%B7%E9%94%AE.html#31-%E8%AE%BE%E7%BD%AE%E6%97%A0%E5%88......
  • React报错之React Hook useEffect has a missing dependency
    正文从这开始~总览当useEffect钩子使用了一个我们没有包含在其依赖数组中的变量或函数时,会产生"ReactHookuseEffecthasamissingdependency"警告。为了解决该错误,禁......
  • 基于Apache Hudi构建分析型数据湖
    为了有机地发展业务,每个组织都在迅速采用分析。在分析过程的帮助下,产品团队正在接收来自用户的反馈,并能够以更快的速度交付新功能。通过分析提供的对用户的更深入了解,营......
  • 一文搞懂 Ftrace 的实现原理
    arm64栈帧结构arm64有31个通用寄存器r0-r30,用法分别如下:寄存器意义SPStackPointer:栈指针r30LinkRegister:在调用函数时候,保存下一条要执行指令的......
  • Anaconda, PyTorch, CUDA Driver, PyCharm 安装与配置
    1安装Anaconda(2022.05)最新版本https://www.anaconda.com/历史版本https://repo.anaconda.com/archive/打开安装包:nextIAgreeJustMe(影响之后创建虚拟环境的......
  • CSS white-space 属性
    https://www.runoob.com/cssref/pr-text-white-space.html属性值值描述normal默认。空白会被浏览器忽略。pre空白会被浏览器保留。其行为方式类似HTML中的<p......
  • Nik Collection 5 for Mac(PS滤镜插件套装)中文版
    NikCollection5formac是一款功能强大的图片处理插件集,其包含了七款ps插件,功能涵盖修图、调色、降噪、胶片滤镜等方面。NikCollection作为很多摄影师和摄影爱好者所熟......
  • Bridge 2022 for Mac(br 2022文件管理软件)中文版
    Bridge2021forMac是一款组织工具应用,从AdobeBridge2021中可以查看、搜索、排序、管理和处理图像文件,还可以使用AdobeBridge2021mac中文版来创建新文件夹、对......
  • Adobe Bridge 2022(Br2022)mac/win
    AdobeBridge 是AdobeCreativeSuite的控制中心。您使用它来组织、浏览和寻找所需资源,用于创建供印刷、网站和移动设备使用的内容。AdobeBridge使您可以方便地访问本......