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. vs2005 测试 lua环境

    (1)添加文件核路径 (2)库文件路径 (3)main.cpp #include <stdio.h>#include <string.h> extern "C&quo ...

  2. Jquery html页面处理基础

    1.怎样获得浏览器的可视高度?   var windHight = $(window).height(); //获得浏览器的可视高度 2.怎样获得滚动条相对于顶部的高度?   var scrollHi ...

  3. Hibernate、批量操作数据

    Hibernate 批量操作数据可以使用两种方法实现 1.分批更新,每一小批同步一次数据: public void saveEmployee2(){ Session s=HibernateSessio ...

  4. javascript第四课变量作用域

    局部变量: function f1() { var n1=0;  //局部变量 n1=10; //全局变量,当前页面均可调用 } n1=10;//全局变量 var n1=10;//全局变量 在方法内的 ...

  5. (转)IOS笔记 #pragma mark的用法

    简单的来说就是为了方便查找和导航代码用的.   下面举例如何快速的定位到我已经标识过的代码.     #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...

  6. ODBC与JDBC比較

    在学习J2EE的JDBC过程中,刚见到JDBC就立即联想到了ODBC,并且我们能够肯定他们之间有必定的关系.開始学它的时候还是认为有点晕,于是就查了非常多资料,与比較熟悉的ODBC进行了比較. 先各自 ...

  7. JS 事件绑定的几种方式 小笔记

    第一种 var test=document.getElementById('add'); add.onclick=function(){ alert('1'); } 直接在对象上注册事件 缺点:如果我 ...

  8. 自学Xpath的几个例子

    Xpath可以对XML文件中的信息进行查找,并对XML文件树形结构进行遍历.详细语法请转:http://www.w3school.com.cn/xpath/index.asp 例子:利用在JavaSc ...

  9. java日期处理 calendar 和date

    抄的人家的,仅作学习使用. java Date获取 年月日时分秒     package com.util;   import java.text.DateFormat; import java.ut ...

  10. vs2010打开设计器出现错误

    vs2010打开设计器出现此界面,  错误多种,还有“未将对象引用设置到对象的实例”  ,我项目用到了第三方控件(没有安装,bin文件夹导入DLL文件,项目直接引用的DLL文件),看下面的堆栈信息,显 ...