Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 68211    Accepted Submission(s): 23017

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

//题意:多个(<=10000)模式串(len<=50),一个匹配串(len<10^6),求匹配数

//AC自动机模板题

指针

 #include <bits/stdc++.h>
using namespace std;
#define MX 1000005 struct Node{
Node * nex[];
Node * fail;
int sum;
Node(){
for (int i=;i<;i++) nex[i]=NULL;
fail=NULL; sum=;
}
}*root; int n, cnt;
char str[MX]; void Init(){
root=new(Node);
} void Insert(char *s)
{
int len = strlen(s);
Node * p=root;
for (int i=;i<len;i++)
{
int x = s[i]-'a';
if (p->nex[x]==NULL) p->nex[x]=new(Node);
p=p->nex[x];
}
p->sum++;
} void Build_fail()
{
queue<Node *> Q;
Q.push(root);
while (!Q.empty())
{
Node *p = Q.front(); Q.pop();
for (int i=;i<;i++)
{
if (p->nex[i])
{
if (p==root)
p->nex[i]->fail=root;
else
{
Node * tmp = p->fail;
while (tmp)
{
if (tmp->nex[i])
{
p->nex[i]->fail=tmp->nex[i];
break;
}
tmp = tmp->fail;
}
if (tmp==NULL) p->nex[i]->fail=root; //
}
Q.push(p->nex[i]);
}
}
}
} void AC_auto(char *s)
{
int len = strlen(s);
Node *p=root, *tmp;
for (int i=;i<len;i++)
{
int x = s[i]-'a';
while (p->nex[x]==NULL && p!=root) p=p->fail;
p=p->nex[x];
if (!p) p=root;
tmp = p;
while (tmp)
{
if (tmp->sum>=)
{
cnt+=tmp->sum;
tmp->sum=-;
} else break;
tmp=tmp->fail;
}
}
} void _delete(Node *p)
{
for (int i=;i<;i++)
{
if (p->nex[i])
_delete(p->nex[i]);
}
delete(p);
} int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
Init();
for (int i=;i<=n;i++)
{
scanf("%s",str);
Insert(str);
}
scanf("%s",str);
cnt = ;
Build_fail();
AC_auto(str);
printf("%d\n",cnt);
_delete(root);
}
return ;
}

Keywords Search(AC自动机模板)的更多相关文章

  1. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

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

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

  3. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  4. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

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

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

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

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

  7. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  8. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  9. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

随机推荐

  1. 【CI】系列一:总体环境规划

    上周花了点时间把CI环境再次给搞起来了,但是觉得在实体机中总觉得不是很安心,安全性不足,另外没有做备份,安全性.扩展性等都不足,且不好迁移. 因为目前只给了我一台PC及,配置其实也不怎么样.但是却需要 ...

  2. python3自己主动爬笑话

    学校的server能够上外网了,所以打算写一个自己主动爬取笑话并发到bbs的东西,从网上搜了一个笑话站点,感觉大部分还不太冷.html结构例如以下: watermark/2/text/aHR0cDov ...

  3. Java并发包——Blockingqueue,ConcurrentLinkedQueue,Executors

    背景 通过做以下一个小的接口系统gate,了解一下mina和java并发包里的东西.A系统为javaweb项目,B为C语言项目,gate是本篇须要完毕的系统. 需求 1. A为集群系统,并发较高,会批 ...

  4. 微信小程序 的文字复制功能如何实现?

    text设置属性  selectable="true" 就可以长按复制了 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...

  5. JavaScript完整性检查

    1.7个“坑” <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...

  6. InputStream写文件出现大量NUL

    写文件大家出现最多的是汉字知码之类的问题,今天不是乱码问题,是出现在大量空字符,用记事本打开是不可见的.如果用NodePad++打开则会显示NUL 问题分题: 刚开始以为是编码问题,试了几个编码发现问 ...

  7. android-pull方式解析xml文件以及XML文件的序列化

    android解析XML ---------------------------基础要像磐石 在android平台上可以使用SAX.DOM和自带的Pull解析器解析xml文件,本文主要介绍使用pull ...

  8. iframe元素获取

    应用场景:main.jsp 中有Iframe,其中包含 home.jsp,在main.jsp中的dom元素绑定的方法操作home.jsp中元素.或反之操作.

  9. asp.net core mvc视频A:笔记5-1.路由规则

    方法一:通过MapRoute方法,配置文件位置 小例子:如果所有路径都要在admin下,可以这样写 方法二:通过路由属性 相对路由 现在需要加/admin/home/index才能正常访问原来的默认页 ...

  10. centos 启动 nginx

    service nginx start https://jingyan.baidu.com/article/bad08e1ec2adc709c85121aa.html