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. MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在

    问题描述:在用vs生成MVC时若使用Internet应用程序为模版,项目建好后重新编译下无法通过,弹出错误: 解决方案:问题出来后,询问了身边很多人都是一头雾水,于是乎各种谷歌和百度,还好功夫不负有心 ...

  2. knockout 与checkbox联动

    knockout 通过teplate实现简单的代码实现复杂的操作绑定checkbox,代码如下自我感觉很赞!!! 前台HTml <ul data-bind="template: { n ...

  3. Oracle EBS-SQL (WIP-9):检查车间任务超发料.sql

    select WE.WIP_ENTITY_NAME                                  任务号,         MFG_LOOKUPS_WJS.MEANING      ...

  4. 非GUI-Qt程序运行后显示Console(简单好用)

    ----我的生活,我的点点滴滴!! 有很多时候,我们在程序中添加了好Debug信息,方便程序在运行期间打印出一些我们需要的信息或者,想用他来显示一些必要信息时, 那么console就太重要了,曾几何时 ...

  5. 剑指offer32 整数中1出现的次数(从1到n整数中1出现的次数)

    class Solution { public: int NumberOf1Between1AndN_Solution(int n) { ) ; ; ; while(i<=n) { int p= ...

  6. mvc mvp mvvm区别

    1 mvc是有视图(view),控制器(controller),模型(model)组成 view(用户界面) controller (业务逻辑)            model(数据存储) 接受指令 ...

  7. doGet与doPost的区别

    转自:http://blog.csdn.net/luoweifu/article/details/7865243   目录(?)[-] 不同点一 不同点二 输入表单inputhtml Serlvlet ...

  8. EassyMock实践 捕获参数

    在测试接口过程中,有时我们希望知道自己期望传入的参数是什么,以此来判断传入参数的正确行,这时就需要用到EassyMock的capture方法.该方法能捕获传入的参数存放到自定义的变量中,然后用捕获的参 ...

  9. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

  10. leetcode Remove Duplicates from Sorted Array python

    class Solution(object): def removeDuplicates(self,nums): if len(nums) <= 0: return 0 j=0 for i in ...