Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 59360    Accepted Submission(s): 19520

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
 
Author
Wiskey
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 

分析:

AC自动机模板题...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std; const int maxn=1000000+5,maxm=50+5; int n,cas,tot,head,tail,q[500000+5]; char s[maxn],word[maxm]; struct trie{
int cnt,fail,nxt[26];
}tr[500000+5]; inline void init(void){
for(int i=0;i<=tot;tr[i].fail=-1,tr[i].cnt=0,i++)
for(int j=0;j<26;j++)
tr[i].nxt[j]=0;
tot=0;
} inline void insert(char *word){
int p=0,len=strlen(word);
for(int i=0;i<len;i++){
if(!tr[p].nxt[word[i]-'a'])
tr[p].nxt[word[i]-'a']=++tot;
p=tr[p].nxt[word[i]-'a'];
}
tr[p].cnt++;
} inline void buildACM(void){
head=0,tail=0;q[0]=0;
while(head<=tail){
int id=q[head++],p=-1;
for(int i=0;i<26;i++)
if(tr[id].nxt[i]){
if(id){
p=tr[id].fail;
while(p!=-1){
if(tr[p].nxt[i]){
tr[tr[id].nxt[i]].fail=tr[p].nxt[i];
break;
}
p=tr[p].fail;
}
if(p==-1) tr[tr[id].nxt[i]].fail=0;
}
else
tr[tr[id].nxt[i]].fail=0;
q[++tail]=tr[id].nxt[i];
}
}
} inline int query(void){
int i=0,ans=0,index,len=strlen(s),p=0;
while(s[i]){
index=s[i]-'a';
while(!tr[p].nxt[index]&&p) p=tr[p].fail;
p=tr[p].nxt[index];
int tmp=p;
while(tmp&&tr[tmp].cnt!=-1)
ans+=tr[tmp].cnt,tr[tmp].cnt=-1,tmp=tr[tmp].fail;
i++;
}
return ans;
} signed main(void){
scanf("%d",&cas);
while(cas--){
tot=500000;init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",word),insert(word);
buildACM();
scanf("%s",s);
printf("%d\n",query());
}
return 0;
}

  


By NeighThorn

HDOJ 2222: Keywords Search的更多相关文章

  1. AC自动机 HDOJ 2222 Keywords Search

    题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零.   新模板,加上last跑快一倍 #include <bits/stdc++.h> ...

  2. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  4. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  5. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. HDU 2222 Keywords Search(瞎搞)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  8. hdu 2222 Keywords Search 模板题

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. 【HDOJ】2222 Keywords Search

    AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...

随机推荐

  1. Linux分享笔记:查看帮助命令 & 常用系统工作命令

    在执行命令时,为了防止出现权限不足的问题,在登陆Linux系统时,要点击普通用户名下的 “Not listed?” 用root管理员身份登陆. [1] 执行查看帮助命令 man 这条命令用来查看帮助文 ...

  2. session在C#一般处理程序的调用方式

    在C#中有一个一般处理程序,可以快速地进行一些逻辑运算等功能,但在这个页面上,不能直接选择使用session进行页面间的值的传递,如何使得页面可以使用session呢 在页面开头写上 using Sy ...

  3. 爬取代理IP,并判断是否可用。

    # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_all() import urllib2 from gevent.pool ...

  4. mount: no medium found on /dev/sr0 找不到介质

    在VMware虚拟机中配置yum源时,执行 mount /dev/cdrom /mnt/cdrom 出现 mount: no medium found on /dev/sr0. 首先在/mnt 目录下 ...

  5. Linux下面自动清理超过指定大小的文件

    Linux下面自动清理超过指定大小的文件 思路:1)查找test目录下的所有的文件2)判断是否大于100M3)大于100M则清空 以byte为单位显示文件大小,然后和100M大小做对比. 100M换算 ...

  6. python变量声明及简单数据类型

    一.python声明变量 变量的命名和使用 # 在Python中使用变量时,需要遵守一些规则和指南. # 违反这些规则将引发错误,而指南旨在让你编写的代码更容易阅读和理解.请务必牢记下述有关变量的规则 ...

  7. [译]The Python Tutorial#10. Brief Tour of the Standard Library

    [译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...

  8. js中正则表达式与Python中正则表达式的区别

    今天女票让我帮她写一个js中的正则,来提取电话号码,对于正则规则来说,js与python是基本没有区别的,重点的区别是在一些函数与方法中. python中的正则提取: import re str = ...

  9. A1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  10. Appium运行时没有启动activity的权限:A new session could not be created.(Original error: Permission to start activity denied)

    小白搞appium,遇到启动不了activity的问题: 查找解决方案说是跟AndroidManifest.xml有关系,参考:https://github.com/appium/appium/iss ...