C++之路进阶——hdu2222(Keywords Search)
/*
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 47794 Accepted Submission(s): 15228
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
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; char s[],m[];
int T,n,sz,ans;
int a[][]/*字典树*/ ,q[],point[],danger[]/*单词结束标志*/;
bool mark[]; int ins()
{
int now=,l=strlen(s);
for (int i=;i<l;i++)
{
int t=s[i]-'a'+;
if (a[now][t]) now=a[now][t];
else now=a[now][t]=++sz;
}
danger[now]++;
} int acmach()
{
q[]=;point[]=;
int now,w=,t=;
while (t<w)
{
now=q[t++];
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];
q[w++]=a[now][i];
}
}
} int solve()
{
int k=,l=strlen(m);
for(int i=;i<l;i++)
{
mark[k]=;
int t=m[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--)
{
ans=;
sz=;
scanf("%d",&n);
for(int i=;i<=;i++)a[][i]=;
for (int i=;i<n;i++)
{
scanf("%s",s);
ins();
}
scanf("%s",m);
acmach();
solve();
for(int i=;i<=sz;i++)
{
point[i]=danger[i]=mark[i]=;
for(int j=;j<=;j++)
a[i][j]=;
}
}
}
C++之路进阶——hdu2222(Keywords Search)的更多相关文章
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
- 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 ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- window对象;document对象;
window对象: DOM:文档对象模型 --树模型文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西 一:window: 属性(值或者子对象):opener:打开当前窗口的源窗口,如果当前 ...
- Java进程间通信
传统的进程间通信的方式有大致如下几种: (1) 管道(PIPE) (2) 命名管道(FIFO) (3) 信号量(Semphore) (4) 消息队列(MessageQueue) (5) ...
- 【android学习1】:安装MySQL启动服务失败解决方法
最近需要用到MySQL,从官网上下载了一个安装文件,但是安装时一直弹出如下提示信息: Configuration of MySQL Server 5.7 is taking longer than e ...
- android常用命令
首先配置好环境变量会比较方便... 大部分sdk提供的工具都在sdk\platform-tools和sdk\tools下,建议配置这两个路径到path 另外aapt工具在sdk\build-tools ...
- 常用jQuery代码02
一.each函数拿到每个元素的宽度 setTimeout(function () { $(".sticker_list img").each(function () { var W ...
- Average Cost (AVCO) Method
http://accountingexplained.com/financial/inventories/avco-method Average Cost (AVCO) Method Aver ...
- 利用css做扇形
html和css每一块的边边角角都是直来直去,除了border-raius,要怎么做扇形了?当然,你如果只想要得到直角扇形,和半圆,那就很简单?那么做小于180的直角扇形,如何做了(大于180的直角无 ...
- JavaScript:JavaScript中常见获取对象元素的方法
介绍: javascript中常见的3种获取元素的方法,分别是通过元素ID.通过标签名字和通过类名字来获取 操作如下: 1.getElementById DOM提供了一个名为getElementByI ...
- Linux命令行–理解Linux文件权限(转)
6.1.1 /etc/passwd文件 /etc/passwd:包含系统用户账户列表以及每个用户的基本配置信息 每个条目有七个字段,每个字段用冒号隔开 登录用户名 用户密码 用户账户的UID 用户账户 ...
- APICloud上openFrameGroup把菜单挡住了,怎么处理?
问:openFrameGroup把菜单挡住了,怎么处理? 试了sendFrameToBack没反应,又不能页面自己openFrameGroup,不知道该怎么办.而且用另外的页面先openFrameGr ...