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. 【转载】Linux的进程间通信-信号量

    原文:Linux的进程间通信-信号量 Linux的进程间通信-信号量 版权声明: 本文章内容在非商业使用前提下可无需授权任意转载.发布. 转载.发布请务必注明作者和其微博.微信公众号地址,以便读者询问 ...

  2. 【转载】ogre内存管理

    原文:ogre内存管理 OGRE内存分配策略相关文件及简述 OGRE提供了自己的内存分配策略,甚至为STL容器提供了新的分配策略,相关文件及简述如下: OgreMemoryAllocatedObjec ...

  3. Intellij IDEA中使用Struts2

    据说struts2中有很多的漏洞, 不过作为学习我也就用了吧, 因为书上面是按着这个讲的呀. 难怪官网上也没有struts2.2.1的版本的下载. 1. 下载struts2.2.1 ga版本 2. 新 ...

  4. emacs学习

    (set-default-font "Consolas-14")      // 设置字体及其大小 M-数字 命令 C-数字 命令

  5. ZOJ 3785 What day is that day?(今天是星期几?)

    Description 题目描述 It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? 今天是星期六,11 + ...

  6. python_way ,day22 tonardo

    python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...

  7. 【网摘】DICOM 基础简介

    一 什么是DICOM?DICOM是Digital Imaging and Communication of Medicine的缩写,是美国放射学会(American College of Radiol ...

  8. 利用CGLib实现动态代理实现Spring的AOP

    当我们用Proxy 实现Spring的AOP的时候, 我们的代理类必须实现了委托类的接口才能实现. 而如果代理类没有实现委托类的接口怎么办? 那么我们就可以通过CGLib来实现 package cn. ...

  9. 从网页(WEB)登录SAP

    以下这篇文章写得很详细,照着做就可以了: http://www.doc88.com/p-293361232332.html   设好后, 默认的端口是80$$, 其中$$是安装SAP时的instanc ...

  10. DZY Loves Chessboard

    DescriptionDZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m ...