【HDU2222】Keywords Search AC自动机
【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
Smple Output
3
题意:给出一堆单词和一个字符串,问有多少个单词在字符串中出现过。
题解:AC自动机第一道模板题,我没有用指针,结果也AC了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int ch[26],fail,cnt;
}p[250000];
int n,tot,len,ans;
char str[1000010],w[60];
queue<int> q;
void build() //构建fail指针
{
int i,u,t;
q.push(1);
while(!q.empty())
{
u=q.front(),q.pop();
for(i=0;i<26;i++)
{
if(!p[u].ch[i]) continue;
q.push(p[u].ch[i]);
if(u==1)
{
p[p[u].ch[i]].fail=1; continue;
}
t=p[u].fail;
while(!p[t].ch[i]&&t) t=p[t].fail;
if(t) p[p[u].ch[i]].fail=p[t].ch[i];
else p[p[u].ch[i]].fail=1;
}
}
}
void search() //查找
{
int i,j,u,t;
scanf("%s",str);
len=strlen(str);
u=1;
ans=0;
for(i=0;i<len;i++)
{
while(!p[u].ch[str[i]-'a']&&u!=1) u=p[u].fail;
u=p[u].ch[str[i]-'a'];
t=u=(u>0)?u:1;
while(t!=1)
{
if(p[t].cnt>=0) ans+=p[t].cnt,p[t].cnt=-1; //防止重复计数
else break;
t=p[t].fail;
}
}
printf("%d\n",ans);
}
void work()
{
scanf("%d",&n);
tot=1;
memset(p,0,sizeof(p));
int i,j,k,t;
for(i=1;i<=n;i++)
{
scanf("%s",w);
k=strlen(w);
t=1;
for(j=0;j<k;j++) //构建Trie树
{
if(!p[t].ch[w[j]-'a']) p[t].ch[w[j]-'a']=++tot;
t=p[t].ch[w[j]-'a'];
}
p[t].cnt++;
}
build();
search();
}
int main()
{
int T;
scanf("%d",&T);
while(T--) work();
return 0;
}
【HDU2222】Keywords Search AC自动机的更多相关文章
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
随机推荐
- ios显示一个下载banner
<meta name="apple-itunes-app" content="app-id=432274380" /> 这个标签是告诉iphone的 ...
- Webview和Html
参考http://blog.csdn.net/chenzheng_java/article/details/6260872 <html><header> <title&g ...
- TKT中文编程语言简介
TKT中文编程语言简介 TKT语言是新型的类似自然语言的汉语编程语言. 它是基于新的语言设计思想创造的语言,和现存的易语言.习语言.O语言.汉编等中文编程语言没有关系. TKT语言特点一: 中文编程 ...
- linux几个常用的命令及获取帮助的方法
date:时间管理,可以显示.修改系统时间. 设定时间:格式:MMDDhhmm[[cc]yy][.ss]其中MM为月分,DD为日期,hh为小时,mm为分钟,CC为年的前两位 YY为年分的后两位,.s ...
- JavaScript中的 offset, client,scroll
在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...
- Javascript权威指南学习笔记
第二章:词法结构 ;function a(){alert(2)};//前面的分号保证正确地语句解析 第三章:类型.值和变量 基本概念: 1.数据类型---能够表示并操作的值的类型叫做数据类型. 2.变 ...
- 用遗传算法GA改进CloudSim自带的资源调度策略
首先理解云计算里,资源调度的含义: 看了很多云计算资源调度和任务调度方面的论文,发现很多情况下这两者的意义是相同的,不知道这两者是同一件事的不同表述还是我没分清吧,任务调度或者资源调度大概就是讲这样一 ...
- module 和 module.exports 的区别
自己理解的不好,推荐一篇文章吧: http://www.cnblogs.com/pigtail/archive/2013/01/14/2859555.html
- ubuntu 加载新硬盘或分区
查看目前挂载情况 df -lh 查看新的空间或硬盘 fdisk -lu 新硬盘分区 fdisk /dev/sda 输入m 根据提示输入n(创建一个分区) 然后输入e(扩展分区) 输入分区数1 然后指定 ...
- 20. Valid Parentheses
1.题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...