【HDU2222】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.
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的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读3
Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong 在前 ...
- 【hdu 2222】Keywords Search
[题目链接] 点击打开链接 [算法] 此题是AC自动机模板题 AC自动机是很神奇的算法,简单点来说,就是在一棵字典树上进行KMP,它的应用范围很广,非常实用 这篇博客写得很好,推荐阅读 : http: ...
- 【AC自动机】Keywords Search
[题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- C++之路进阶——hdu2222(Keywords Search)
/*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
随机推荐
- Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果
Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 目前Edge的功能尚 ...
- [改善Java代码]在switch的default代码块中增加AssertionError错误
switch的后跟枚举类型,case后列出所有的枚举项,这是一个使用枚举的主流写法,那留着default语句似乎没有任何作用了,程序已经列举出了所有的可能选项,肯定不会执行到default语句,. 错 ...
- hdu 2838 树状数组
思路:从后面往前面插,用一个二维树状数组保存,c[i][0]表示比i小的元素和,c[i][1]表示比i小的元素个数. #include<iostream> #include<cstr ...
- IOS微信中看文章跳转页面后点击返回无效
经过查找原因发现,下面两种链接,链接1返回不了,链接2可以返回. 链接1:http://mp.weixin.qq.com/s?__biz=MzA5NDY5MzcyNA==&mid=265089 ...
- webView中支持input的file的选择和alert弹出
alert()弹出 input的file现选择(特别说明:不同的android版本弹出的样式不同,选择文件后自动上传) webView.setWebChromeClient(new WebChrome ...
- 学习ajax 总结
一.服务器客户端基础知识 通信是指不同计算机程序的通信,单单通过ip地址就能知道你找的是哪一台计算机,但是不知道是计算机上的哪个应用程序,要想知道是哪个程序就必须通过端口.这时候就可以问端口到底是什么 ...
- jqueryeasyui中文乱码问题
下载的Demo中charset=utf-8,手动改成gb3212,问题解决.
- CSS之鼠标经过字体光标形状的改变
CSS中的cursor属性是规定要显示的光标的类型(形状). 参阅JavaScript正则表达式 default 默认光标(通常是一个箭头) auto 默认.浏览器设置的光标. crosshair 光 ...
- iOS - 字典(NSDictionary)
1. 字典类型的常用处理 //---------------不可变字典 //1.字典的创建 NSArray *array1 = [NSArray arrayWithObjects:@"zha ...
- Cocos2d-x优化中多线程并发访问
多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程.源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,Autor ...