【HDU2222】Keywords Search(AC自动机)
Problem DescriptionIn 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.InputFirst 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.OutputPrint how many keywords are contained in the description.Sample Input15shehesayshrheryasherhsSample Output3
4 5
she
he
say
shr
her
yasherhs 5
she
he
say
shr
her
yasherhs 3
she
she
she
shesheshe 6
she
he
he
say
shr
her
yasherhs 答案:
3
3
3
4
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 800100
#define Maxl 60 char s[Maxl];
char ss[]; struct node
{
int son[],cnt,fail;
}t[Maxn];int tot; void upd(int x)
{
t[x].cnt=;
memset(t[x].son,,sizeof(t[x].son));
} void read_trie()
{
int n;
scanf("%d",&n);
tot=;upd();
for(int i=;i<=n;i++)
{
int now;
scanf("%s",s+);
int len=strlen(s+);
now=;
for(int j=;j<=len;j++)
{
int ind=s[j]-'a'+;
if(!t[now].son[ind])
t[now].son[ind]=++tot,upd(tot);
now=t[now].son[ind];
if(j==len) t[now].cnt++;
}
}
} queue<int > q;
void build_AC()
{
int i,j,x,y;
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
x=q.front();
y=t[x].fail;
for(j=;j<=;j++)
{
if(t[x].son[j])
{
q.push(t[x].son[j]);
t[t[x].son[j]].fail=x?t[y].son[j]:x;
}
else t[x].son[j]=t[y].son[j];
}
q.pop();
}
} int gmark(int x,int y)
{
if(!x) return y;
if(t[x].cnt) y+=t[x].cnt,t[x].cnt=;
return gmark(t[x].fail,y);
} void ffind()
{
scanf("%s",ss+);
int l=strlen(ss+);
int now,ans=;
for(int i=;i<=l;i++)
{
int x=ss[i]-'a'+;
now=t[now].son[x];
ans+=gmark(now,);
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
read_trie();
build_AC();
ffind();
}
return ;
}
[HDU2222]
2016-06-14 17:02:09
【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】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- [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 ...
随机推荐
- C++数据结构和算法每天一练(线性表)
#include <iostream> using namespace std; class ArrayLinerTable { public: void InitLine ...
- iOS 网络编程:socket
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- Python爬虫获取知乎图片
前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是Python的简单方便,于是做了点尝试. #coding=utf-8 import urllib import re def getHtml(ur ...
- php之常量小见
php设置常量有二种方法,一为define(),二为使用关键字const. define()函数带有三个形参,一为常量名,通常以大写字母命名,二为值,三为是否对大小写敏感,其值为可选,默认为false ...
- WisDom.Net 框架设计(五) 权限设计
WisDom.Net --权限设计 1.需求分析 基本在所有的管理系统中都离不开权限管理.可以这么说,权限管理是管理系统的核心所在. 权限管理说白一些就是每个人能够做什么,不能够做什么.可以说 ...
- (转)Asp.net的HttpCookie写入汉字读取时为乱...
今天有个问我:在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cook ...
- C#结构内存布局介绍
转载:http://www.csharpwin.com/csharpspace/10455r2800.shtml 本来打算写一篇文章,详细地讨论一下结构的内存布局,但是想了下,跟路西菲尔的这篇文章也差 ...
- 计算机网络-ip地址聚合后可用的地址数
(1)59.81.1.128/28=59.81.1.1000-0000(2)59.81.1.144/28=59.81.1.1001-0000(3)59.81.1.160/28=59.81.1.1010 ...
- javascript类继承系列二(原型链)
原型链是采用最主要的继承方式,原理:每一个类(构造器,js中的function)都有一个原型属性(prototype)指向一个原型对象,原型对象有一个构造器(constructor),它又指回到fun ...
- mysql的账户失效,之前的密码无法登录
引用自:http://blog.sina.com.cn/s/blog_682c287b0100ofz8.html 此为linux服务器下的做法 方法一: 1.关闭mysql # service ...