【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 ...
 
随机推荐
- sort()排序 collections.sort();
			
1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...
 - Zookeeper3.4.6部署伪分布集群(Apache)
			
1.下载Zookeeper http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.6/ 2.创建/usr/app/zookeeper目录,并切换 ...
 - 【转】warning C4819,该文件保存为 Unicode 格式以防止数据丢失,处理方法
			
以下的解决方案只是把错误给屏蔽掉而已,并不能真正解决这个警告.仅供参考! 当项目引用到外部源代码后,经常出现4819错误,警告信息如下: warning C4819: 该文件包含不能在当前代码页(93 ...
 - thinking in java 读书笔记 --- overriding private method
			
一个对象可以被用作它自身的类型或者是它的基类类型.当用作它的基类类型时叫做upcasting,如下所示 BaseClass ref = new DerivedClass() //upcasting ...
 - WTL 自绘 进度条Progressbar
			
WTL 绘制的进度条,逻辑清晰明了,代码函数清晰易懂:基本思路就是 首先绘制 进度条背景图,然后根据动态进度不断重绘前景进度条,绘制操作在OnPaint函数里画.该类可以直接用于项目中. 使用示例: ...
 - Oracle 汉字在不同字符集下所占字节
			
今天发现了一个问题,一个长度设置为2000字节的字段,插入一条长度为1000的汉字数据,竟然报错了. 一个汉字占两个字节,按理说刚好是2000个字节.但通过查看日志,发现插入数据的长度为3000字节. ...
 - .NET设计模式(4):建造者模式(Builder Pattern)
			
):建造者模式(Builder Pattern) .建造者模式的使用使得产品的内部表象可以独立的变化.使用建造者模式可以使客户端不必知道产品内部组成的细节. 2.每一个Builder都相对独立, ...
 - Masonry等比缩放
			
第一种: CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]); CGFloat aspectRatio = 1 ...
 - Android运行异常情况分析(持续更新)
			
1.java.lang.IllegalAccessException: access to class not allowed 原因:在写class 文件的时候没有把class设置成public 2. ...
 - 嵌入式web server——Goahead移植要点
			
前言 在嵌入式设备中,在没有液晶显示的情况下,可以使用web来访问设备,查看设备的运行状态以及进行参数设置,类似于路由器设置.网上有很多关于各种web server的优劣的评论,在此不讨论,只是介绍其 ...