#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N=1000000+5;
struct node{
int son[26],fail,sum;
bool vis;
}trie[N];
int tot,ans;
void init(){
for(int j=0;j<=tot;j++){
trie[j].fail=trie[j].sum=trie[j].vis=0;
for(int i=0;i<26;i++)
trie[j].son[i]=0;
}
/*
memset(trie,0,sizeof(trie));//这种方法爆内存
trie[0].fail=trie[0].sum=trie[0].vis=0;//这种方法TLE
for(int i=0;i<26;i++)
trie[0].son[i]=0;
*/
tot=ans=0;
}
void Insert(char *s){
int p=0;
for(int i=0,len=strlen(s);i<len;i++){
int t=s[i]-'a';
if(!trie[p].son[t])
trie[p].son[t]=++tot;
p=trie[p].son[t];
}
trie[p].sum++;
}
void build_ac(){
queue<int> Q;
for(int i=0;i<26;i++)
if(trie[0].son[i])
Q.push(trie[0].son[i]);
while(!Q.empty()){
int t=Q.front();Q.pop();
for(int i=0;i<26;i++){
int tmp=trie[t].son[i];
if(!tmp)continue;
int f=trie[t].fail;
while(f&&!trie[f].son[i])
f=trie[f].fail;
trie[tmp].fail=trie[f].son[i];
Q.push(tmp);
}
}
}
int Query(char *str){
int p=0,ret=0;
for(int i=0,len = strlen(str);i<len;i++){
int t=str[i]-'a';
while(p && !trie[p].son[t])
p=trie[p].fail;
p=trie[p].son[t];
int tmp=p;
while(tmp && !trie[tmp].vis){
ret+=trie[tmp].sum;
trie[tmp].vis=true;
tmp=trie[tmp].fail;
}
}
return ret;
}
int T,n;
char s[55],str[N];
int main(){
cin>>T;
while(T--){
init();
scanf("%d",&n);
while(n --){
scanf(" %s",s);
Insert(s);
}
build_ac();
scanf(" %s",str);
printf("%d\n",Query(str));
}
return 0;
}
相关推荐
图片边界矢量化-地理配准
无地理和投影信息的JPG图片,通过ArcGIS进行地理配准(Georeferencing)完成数字化边界。
C++——AC自动机例题——Keywords Search(HDU2222)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 58664 Accepted Submission(s): 19253 Problem Description In the
保姆级教程:用MATLAB复现格拉斯哥大学FMCW雷达数据集多普勒谱(附完整代码与避坑指南)
本文提供了一份详细的MATLAB教程,指导如何复现格拉斯哥大学FMCW雷达数据集的多普勒谱分析。从环境准备、数据获取到距离压缩、MTI滤波、时频分析(STFT)以及结果后处理,涵盖了完整流程,并附有优化代码和避坑指南,特别适合雷达信号处理研究者和工程师。
HDU2222 Keywords Search
题目链接:HDU2222 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 49488 Accepted Submission(s): 15845 Problem Des
SAP 移动类型简述
SAP系统中用于标志物料移动(收货/发货/转储/转储记帐)的三位的数字/字母 通常分几大类:收货 发货 转储 库房调整 常用的有: 收货类 (MB1C) 101 采购订单收货 103 采购订单收货至冻结状态 122 无采购订单退货 161 按退货采购订单退货 131 从生产订单 501 无采购订单收货 511 免费收货 发货类(MB1A) 201 成本中心消耗
VisualStudio2022启动报错
把Users\...\AppData\Local\Microsoft\VisualStudio\11.0\ComponentModelCache中的Microsoft.VisualStudio.Default.cache这个文件移除,再重启vs,各项功能都能正常使用了。第一次使用方法1有效果,不知道什么原因后面又报错再删除缓存文件无效了,最后使用方法2启动VS成功。vs2022启动时,报错弹框,在ActivityLog查看具体报错信息,报错原因未知。网上搜索到两种解决方法。方法1:删除缓存文件。
hdu2222 Keywords Search
AC自动机裸题
想在 Visual Studio Code 里进行 ABAP 开发,需要安装的扩展列表
安装完毕后,我们可以将 SAP 系统配置为 Visual Studio Code 连接的远程文件系统。为了连接到 SAP 系统,需要启用 ABAP 开发工具 (ADT) ICF 节点,这与 SAP Hana Studio 等工具使用的服务相同。打开 SAP GUI 并连接到目标系统,运行事务 SICF,然后激活 ADT_SRV 服务,如下所示。这启用了允许代码编辑器和 IDE 连接到 SAP 系统的服务。
Hdu 2222 Keywords Search
package hdu; import java.util.ArrayDeque; import java.util.Scanner; /** * @description HDU 2222 Keyboards Search. * @technique AC自动机, 字典树。 * @version 1.2 * @date 20120727 * @time 20:2
题解:AcWing 6051 看病
等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。但医院的排队不同其他排队,因为多数情况下,需要病情严重的人优先看病,所以希望BSNY设计系统时,以病情的严重情况作为优先级,判断接下来谁可以去看病。具体是这样的,最近来医院看病的人越来越多了,因此很多人要排队,只有当空闲时放一批病人看病。的操作,输出此时还在排队人中,优先级最大的患者姓名和优先级。后面没有输入,但需要你输出。
视图库对接系列(GA-T 1400)一、简单介绍
GA/T 1400《公安视频图像信息应用系统》 是我们需要参考看看的相关文档,里面有对应的标准和介绍,没有这个文档的话可以加群找我要。群的话 我放在最下面了。我们先简单想几个问题, 也就是这个协议主要干什么用的,为什么要有, 能做什么。
【HDU 2222】Keywords Search
1.题目链接。题目大意:给定多个单词,一段文本,找出这段文本中出现过多少个给定的单词。要是单词只有一个,直接KMP完事,但是单词多个就不行了,这是典型的多模式匹配算法。对于多模式匹配算法,我也是刚刚学了,因为之前字符串算法都是交给其他的队友完成。推荐文章:https://www.cnblogs.com/cmmdc/p/7337611.html。 里面写了AC自动机的详解:其实就是树上KMP完事...
efm32 入门.pdf
本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。
AC自动机 模板 【HDU2222】 Keywords Search
AC自动机(Aho Corasick Automaton) 如果匹配字符串,一个模式串,一个匹配串,KMP就搞定了。 但是如果现在有多个模式串怎么办呢?每个模式串都处理一遍,再与匹配串匹配就太慢了,所以有了AC自动机,来解决这类问题。AC自动机可以看成是Trie树和KMP的结合体,具体的实现方式就是在每个节点处增添一个fail指针,作用与KMP中的失配时用的跳转数组差不多(虽然在匹配的大多数时候
SIMATIC S7-1200, S7-1500 PID 控制[手册]
SIMATIC S7-1200, S7-1500 PID 控制[手册]
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
HDU 2222 Keywords Search【ACAM】
HDU 2222 Kerwords Search 代码风格模仿自:USETC每周算法讲解,AC自动机,郭老师! 输入一个T,对于每一个T给你一个n,接下来输入P个模式串,然后给你一个L串,L中出现了多少P中的串。 这是一道比较基础的AC自动机问题,我只不过是为了不想用板子而直接写,因此写此博文。 将容易遗漏或者写错的地方进行了注释的标注,用于提示自己,也用于与大家进行分享。 #include<...
Data-Structures-and-Algorithm-Analysis-in-Java.pdf
作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...
HDU 2222 Keywords Search
ac自动机第一题 代码参考kuangbin,用数组实现 思想参考的这个,写得很详细:http://www.cppblog.com/mythit/archive/2009/04/21/80633.html #include #include #include #include #include using namespace std; #define N 500010 struct trie
HDU - 2222——Keywords Search
http://acm.hdu.edu.cn/showproblem.php?pid=2222T个测试数据,n个关键字,问所给字符串出现了哪几种关键字。HDU的测试数据比较弱,提供一组数据1 2 sher he sher输出 2.ac代码。#include<iostream> #include<algorithm> #include<cstring> #inclu...
HDU2222Keywords Search
题意:给n1000000的模式串。 用模式串建立好自动机然后在模式串上搜索就好了 #include #include #include #include #include #include using namespace std; struct Tree { int next[500010][26],fail[500010],end[500010]; int root,L; int
hdu 2222 Keywords Search
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Online Acmers Forum | Discuss Statistical Charts Problem Archive Realtime Judge Status Authors Ranklist C/C++/Java Exams ACM Steps Go to Job Contest Live
HDU2222-Keywords Search AC自动机
HDU2222-Keywords Search AC自动机算法
HDU2222-Keywords Search
Keywords Search Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Ev...
Hdu2222Keywords Search
Hdu2222存一发AC自动机模板…【代码】#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #define N 500005 #define INF 1LL<<60 using namespace std; typedef long long ll; ll r
HDU-2222 Keywords Search 【题解】
题目链接:HDU-2222 或 Vjudge 简单说明: 题意是按行给出n个串,再给一个文本,问在文本中出现了串中的几个。题目没强调串是互不相同的哦! ac自动机的建立,其中插入过程借助了字典树,处理回溯数组(也有人称失败数组)过程是一个广搜运用了STL的队列(queue)。ac自动机的过程还在写。 这是学习ac自动机的第一题,如果wa的话,那就要注意字串结束标记...
HDU2222 Keywords Search + HDU2896病毒侵袭 AC自动机
虽然看明白了KMP和字典树,但是AC自动机还是没怎么理解QAQ... 附上讲解博客Orz:https://blog.youkuaiyun.com/qq_30346729/article/details/78835040 https://www.cnblogs.com/cjyyb/p/7196308.html 1.HDU2222 Keyword...
HDU-2222 Keywords Search
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意: 给你很多个单词,然后给你一篇文章,问给出的单词在文章中出现的次数。 解题思路: AC自动机入门题。需要注意的就是可能有重复单词,坑死人不偿命!~~~~ 代码如下: #include #include #include #include using namespace st
1904



被折叠的 条评论
为什么被折叠?



