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.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output
Print how many keywords are contained in the description.

Sample Input
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

首先要说一下这个题号,2222,hhh。

其次这是一道AC自动机的裸题,但不知为何我第一次打AC自动机打成了爱吃自动机。。。其实这个名字也挺好

黄学长的代码写的很优美,这里就不再多说些什么了。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char ch[],s[];
int T,n,sz,ans;
int a[][],q[],point[],danger[];
bool mark[]; void insert(){
int len=strlen(ch),now=;
for (int i=;i<len;i++){
int t=ch[i]-'a'+;
if (a[now][t])now=a[now][t];
else now=a[now][t]=++sz;
}
danger[now]++;//判断单词是否出现
} void acmach(){//构建AC自动机
int h=,t=;
q[]=;point[]=;
while (h<t){
int now=q[h++];
for (int i=;i<=;i++){
if (!a[now][i]) continue;
int k=point[now];
while(!a[k][i])k=point[k];
point[a[now][i]]=a[k][i];//这里是失败指针,类似KMP
q[t++]=a[now][i];
}
}
} void solve(){
int len=strlen(s),k=;
for (int i=;i<len;i++){
mark[k]=;
int t=s[i]-'a'+;
while (!a[k][t])k=point[k];
k=a[k][t];
if (!mark[k])
for (int j=k;j;j=point[j]){
ans+=danger[j];
danger[j]=;//因为有多组数据
}
}
printf("%d\n",ans);
} int main(){
scanf("%d",&T);
while (T--){
sz=;ans=;
scanf("%d",&n);
for (int i=;i<=;i++)a[][i]=;
for (int i=;i<=n;i++){
scanf("%s",ch);
insert();
}
acmach();
scanf("%s",s);
solve();
for(int i=;i<=sz;i++){
point[i]=danger[i]=mark[i]=;
for(int j=;j<=;j++)a[i][j]=;
}
}
}

【HDU2222】Keywords Search的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  3. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

  4. 【计算机视觉】Selective Search for Object Recognition论文阅读3

    Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong       在前 ...

  5. 【hdu 2222】Keywords Search

    [题目链接] 点击打开链接 [算法] 此题是AC自动机模板题 AC自动机是很神奇的算法,简单点来说,就是在一棵字典树上进行KMP,它的应用范围很广,非常实用 这篇博客写得很好,推荐阅读 : http: ...

  6. 【AC自动机】Keywords Search

    [题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定  n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...

  7. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  8. C++之路进阶——hdu2222(Keywords Search)

    /*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  9. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

随机推荐

  1. [设计模式]<<设计模式之禅>>关于单例模式

     1 我是皇帝我独苗 自从秦始皇确立了皇帝这个位置以后,同一时期基本上就只有一个人孤零零地坐在这个位置.这种情况下臣民们也好处理,大家叩拜.谈论的时候只要提及皇帝,每个人都知道指的是谁,而不用在皇帝前 ...

  2. 关于cmd模式下切换目录

    cmd下切换目录: 经常犯下的错误一: 在默认路径下输入 cd D: 想切换到D盘但是会出现上面的现象. 正确的的做法是直接输入要转移到的盘符: D:  就可以了. 在这种情况下再输入cd D:

  3. AndroidStudio KeyMap

  4. (转载)Nim游戏博弈(收集完全版)

    Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输.后来,在一份资料上看到,这种游戏称为“拈(Nim)”.据说,它源 ...

  5. SQL Server 2008 错误15023:当前数据库中已存在用户或角色

    解决SQL Server 2008 错误15023:当前数据库中已存在用户或角色,SQLServer2008,错误15023,在使用SQL Server 2008时,我们经常会遇到一个情况:需要把一台 ...

  6. android application类的用法

    android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...

  7. JAVA实现上传下载共享文件

    1.上传下载共享文件需要用到jcifs,先下载相关JAR包(开源项目的源码,demo,文挡.API应有尽有) https://jcifs.samba.org/src/

  8. 设置In_Memery

    alter system set inmemory_size=4G scope=spfile; alter table table_name inmemory; alter table table_n ...

  9. Cocos2d-x优化中图片优化

    在2D游戏中图片无疑是最为重要的资源文件,它会被加载到内存中转换为纹理,由GPU贴在精灵之上渲染出来.它能够优化的方面很多,包括:图片格式.拼图和纹理格式等,下面我们从这几个方面介绍一下图片和纹理的优 ...

  10. Swift静态方法

    与静态属性类似,Swift中还定义了静态方法,也称为类型方法,所谓“类型”是指枚举.结构体和类.静态方法定义的方法也是与静态属性类似的,枚举和结构体的静态方法使用的关键字是static,类的静态方法使 ...