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. 如何使用getopt()函数解析参数

    最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...

  2. SQL Server 空间监测

    数据库文件型: select * from sys.dm_db_file_space_usage;      go                                           ...

  3. EMV标准

    EMV标准是由国际三大银行卡组织--Europay(欧陆卡,已被万事达收购).MasterCard(万事达卡)和Visa(维萨)共同发起制定的银行卡从磁条卡向智能IC卡转移的技术标准,是基于IC卡的金 ...

  4. android4.0 HttpClient 以后不能在主线程发起网络请求

    android4.0以后不能在主线程发起网络请求,该异步网络请求. new Thread(new Runnable() { @Override public void run() { // TODO ...

  5. error: ld returned 1 exit status 和 error:undefined reference

    undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...

  6. poj3650---将一个字符串中的特定字符转换

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int i; w ...

  7. poj1922

    Ride to School Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18704   Accepted: 7552 D ...

  8. debian安装jdk6

    一般用命令 apt-get install sun-java6-jdk ,会报找不到源的错误. vim /etc/apt/sources.list # 於最下方新增此行 deb http://ftp. ...

  9. 佩特来项目经验小集合(2)___组合查询存储过程,报错 &quot;varchar JBID=&#39;&#39; 转换成数据类型 int 时失败&quot;

       今天写一个组合查询的存储过程遇到这样一个问题:在将 varchar 值 'SELECT * FROM View_DLS_WXJD_Customer WHERE 1=1 and JBID ='' ...

  10. 【转】HtmlAgilityPack 之 HtmlNode类

    [转]HtmlAgilityPack 之 HtmlNode类 HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 ...