Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25799    Accepted Submission(s): 8421

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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 9999999
using namespace std; const int MAX=1000000+10;
char a[60],b[MAX]; struct TrieNode{
int num;//记录单词的数量
TrieNode *next[26],*fail;//下一个节点和失败指针
TrieNode(){
num=0;
fail=0;
memset(next,0,sizeof next);
}
}*root; void InsertNode(char *a){
int k=0;
TrieNode *p=root;
while(a[k]){
if(!p->next[a[k]-'a'])p->next[a[k]-'a']=new TrieNode;
p=p->next[a[k++]-'a'];
}
++p->num;
} void Build_AC(){
int k=0;
TrieNode *p=root,*next;
queue<TrieNode *>q;
q.push(p);
while(!q.empty()){//一层一层的构造fail
p=q.front();
q.pop();
for(int i=0;i<26;++i){
if(p->next[i]){
next=p->fail;
while(next){
if(next->next[i]){p->next[i]->fail=next->next[i];break;}
next=next->fail;
}
if(!next)p->next[i]->fail=root;
q.push(p->next[i]);
}
}
}
} int SearchTrie(char *a){
int k=0,sum=0;
TrieNode *p=root,*next;
while(a[k]){
while(!p->next[a[k]-'a'] && p != root)p=p->fail;
p=p->next[a[k++]-'a'];
if(!p)p=root;
next=p;
while(next != root && next->num != -1){//不能用0来判断是否已经寻找过
sum+=next->num;
next->num=-1;//这里注意一定要赋值为负数,不能为0
next=next->fail;
}
}
return sum;
} void Free(TrieNode *p){
for(int i=0;i<26;++i)if(p->next[i])Free(p->next[i]);
delete p;
} int main(){
int T,n;
cin>>T;
while(T--){
root=new TrieNode;
cin>>n;
for(int i=0;i<n;++i){
cin>>a;
InsertNode(a);//插入单词
}
Build_AC();//构造失败指针,也就是建立AC自动机的精髓
cin>>b;
cout<<SearchTrie(b)<<endl;//查询含有的单词数量
Free(root);
}
return 0;
}
/*
10
2
abcdef
bcd
abcdef
1
h
hhhhh
5
bhea
her
he
h
ha
bhera
5
bhea
her
he
h
ha
bhera
*/

hdu2222之AC自动机入门的更多相关文章

  1. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  2. hdu2222 ac自动机入门

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

  3. HDU2222(AC自动机入门题)

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

  4. AC自动机入门

    Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...

  5. HDU 2222 Keywords Search(AC自动机入门)

    题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...

  6. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  7. hdu-2222(ac自动机模板)

    题意:给你一个长度为n的单词表,一个文本串,问你这个文本串中出现了单词表中多少个单词: 解题思路:ac自动机的模板题,可以直接当模板用: 代码: #include<iostream> #i ...

  8. hdu 1277 AC自动机入门(指针版和数组版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...

  9. hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

    /** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...

随机推荐

  1. document.ready()的用法

    1.Jquery是优秀的Javascrīpt框架,$是jquery库的申明,它很不稳定(我就常遇上),换一种稳定的写法jQuery.noConflict(); jQuery(document).rea ...

  2. mysql 存储过程 动态sql例子

    proc:BEGIN ; ; ; ) DEFAULT ''; ) DEFAULT ''; ) DEFAULT '';#插入日志的表,一个活动一张表 #将局部变量转换成会话变量 #动态sql语言只接受会 ...

  3. UVA 116 Unidirectional TSP 经典dp题

    题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小. 用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...

  4. 《Qt编程的艺术》——8.2 显示目录层次

    现在我们准备通过创建一个小程序来获得关于InterView的实践经验,使用QDirModel和拿来就用的view,来在四个不同的view中显示主目录,如图8.5所示.在代码里,除了例行公事先实例化一个 ...

  5. 自己主动更新--下载apk以及提示对话框的实现(3)

    下载apk以及提示对话框的实现 一.步骤: 1. 确定有能够更新的版本号,对话框提醒用户是否进行更新. 2. 选择更新的话,显示下载对话框而且进行下载.否则关闭提示更新对话框. 3. Apk下载完毕后 ...

  6. WildFly8.1(JBoss)+mod_cluster(Apache)群集配置

    继上次使用mod_jk传导Apache+JBoss群集配置后,.因为JBoss5.1启动太慢,于是我开始尝试用最新的WildFly8.1构造(WildFly那是,JBoss.在JBoss7之后改名). ...

  7. Java基础学习笔记2

    运算符: 重点:++和--运算符; a++ (a--):表示先将a的原值带入计算,计算完毕后,再将a的值进行+1(-1); ++a (--a):先将a的值进行+1(-1)运算,然后将+1(-1)以后的 ...

  8. ASP.NET动态引用WebService接口

    尊重原著作:本文转载自http://www.mhzg.net/a/20124/20124912180589.html 有经验的朋友都知道,通常我们在引用webservice的时候,是在项目中就添加了引 ...

  9. 修改oracle内存

    [oracle@bi11g bin]$ ./sqlplus /nolog SQL*Plus: Release 11.2.0.1.0 Production on Fri Dec 1618:39:36 2 ...

  10. Oracle错误ORA-03113: end-of-file on communication channel处理办法

    oracle不能启动了,报错ORA-03113: end-of-file on communication channel (通信通道的文件结尾) 解决办法: SQL> startup ORAC ...