Keywords Search

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

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
 
ac自动机:  构造一颗Trie树,像kmp一样构造一个失败指针,进行记录;
代码:
 #define LOCAL
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct Trie
{
struct Trie *fail;
struct Trie *child[];
int tail; //末尾标记
}; void _insert(char *s,Trie *root) //构造一个Trie树
{
Trie *newcur,*cur;
cur=root;
for(int i=;s[i];i++)
{
if(cur->child[s[i]-'a']==NULL)
{
newcur= new Trie ;
for(int j=;j<;j++)
newcur->child[j]=NULL;
newcur->fail=NULL;
newcur->tail=;
cur->child[s[i]-'a']=newcur;
}
cur=cur->child[s[i]-'a'];
}
cur->tail++; //有可能有重复的单词
} //构造失败指针
void ac_fail(Trie * root)
{
queue<Trie*>tree;
Trie *fro,*q;
tree.push(root);
while(!tree.empty())
{
fro=tree.front();
tree.pop();
for(int i=;i<;i++){
if(fro->child[i]!=NULL)
{
if(fro==root)
fro->child[i]->fail=root; //将他的下一个函数的指针的失败指针指向当前指针
else
{
q=fro;
while(q->fail)
{
if(q->fail->child[i]){
fro->child[i]->fail=q->fail->child[i];
break;
}
q=q->fail;
}
if(!q->fail) fro->child[i]->fail=root;
}
tree.push(fro->child[i]);
}
}
}
} int query(char *s,Trie *root)
{
Trie *cur=root,*newcur;
int ans=;
for(int i=;s[i];i++)
{
while(cur->child[s[i]-'a']==NULL&&cur!=root)
cur=cur->fail;
cur=cur->child[s[i]-'a'];
if(cur==NULL) cur=root;
newcur=cur;
while(newcur!=root&&newcur->tail>)
{
ans+=newcur->tail;
newcur->tail=;
newcur=newcur->fail;
}
}
return ans;
}
char s1[];
char t1[]; //目标主串
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int cas,n;
Trie *root;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
root= new Trie;
for(int i=;i<;i++)
root->child[i]=NULL;
root->fail=NULL;
root->tail=;
while(n--)
{
scanf("%s",s1);
_insert(s1,root);
}
ac_fail(root);
scanf("%s",t1);
printf("%d\n",query(t1,root));
}
return ;
}

hdu----(2222)Keywords Search(ac自动机)的更多相关文章

  1. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

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

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

  3. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  4. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  5. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  6. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

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

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

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

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

  10. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

随机推荐

  1. UVA 1424 二 Salesmen

    Salesmen Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  2. 【转载】linux内核笔记之高端内存映射

    原文:linux内核笔记之高端内存映射 在32位的系统上,内核使用第3GB~第4GB的线性地址空间,共1GB大小.内核将其中的前896MB与物理内存的0~896MB进行直接映射,即线性映射,将剩余的1 ...

  3. oracle, create table, insufficient privileges

    SQL> exec pro_gz_day_report;          ORA-01031: insufficient privileges          ORA-06512: at & ...

  4. [HDOJ4609]3-idiots(FFT,计数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:n个数,问取三个数可以构成三角形的组合数. FFT预处理出两个数的组合情况,然后枚举第三个 ...

  5. 个人简历制作(Dreamweaver)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 二维数组实现checkbox的分组多选

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language="j ...

  7. C#:字段与属性

    MSDN中是这么介绍字段和属性的: A field is a variable of any type that is declared directly in a class or struct. ...

  8. STORM_0010_Message passing implementation/消息传递的实现

    下面是0.8.0之前的表述,之后的已经基于Disruptor改造过了 这个文章演示了发射和转移tuple是怎么在storm中工作的   Worker为消息传递负责 当zk中的任务出现了变化或者每个ta ...

  9. Codeforces Round #279 (Div. 2) C. Hacking Cypher 前缀+后缀

    C. Hacking Cypher time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. XAF实现运行时填加验证规则并保存到数据库中

    有几种方法可以用来声明一个验证规则.最常用的方法是使用对应的Attribute来定义.详见这里.验证模块还允许您通过在业务类实现 IRuleSource 接口定义自定义的验证规则的来源. IRuleS ...