Keywords Search(AC自动机模板)
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
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自动机模板)的更多相关文章
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
随机推荐
- CSDN个人空间、问答频道停站维护公告
各位亲爱的用户: CSDN个人空间.问答频道将于2014年11月17日18点至11月17日24点进行系统维护升级,升级期间会影响大家的正常訪问和操作.给大家带来不便.敬请广大用户谅解 ...
- Selenium webdriver Java 高级应用
对于这一段还蛮有感慨的,只想说,代码还是需要自己去敲的. 1. 改变用户代理 import org.junit.AfterClass; import org.junit.BeforeClass; im ...
- python——深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...
- mui.ajax返回type为abort
最近在使用h5和mui开发app,发现mui.ajax有一个小bug 情况一: 参数和请求路径无误,但是总是调起失败的回调函数,打印出 type=abort (终止请求) 原因: mui.ajax默认 ...
- UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multibyte sequence
[问题] python中已获取网页: http://blog.csdn.net/hfahe/article/details/5494895 的html源码,其时UTF-8编码的. 提取出其标题部分: ...
- mysql调用存储过程出现Illegal mix of collations错误
执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...
- ajaxform 提交,返回JSON时,IE提示下载的问题解决
在使用AJAXform提交表单时,返回的数据格式为JSON,头文件是application/json 时,在 火狐.ie9和谷歌下都能正常解析,在ie7下会提示下载. 解决方法:指定返回页的头文件为& ...
- Linux 查看CPU个数和磁盘个数
top后按数字1,多个cpu的话会显示多个 fdisk -l可以看到多个物理硬盘,做了硬raid只能看到一个硬盘 cat /proc/cpuinfo查看cpu具体的信息
- Verilog HDL test bench 문법에 관한
16bit ripple carry adder test bench `timescale 1ns/1ns module testbench2; reg [15:0] a, [15:0] b, c_ ...
- Angularjs学习笔记1_基本技巧
10.AngularJS ng-click <button ng-click="clickCounter = clickCounter + 1">Click Me! ...