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 ...
随机推荐
- HDU 5379 Mahjong tree(dfs)
题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...
- plsql连接oracle数据库
步骤 (1)线上安装oracle数据库(已配好) (2)本地远程连接.安装oracle客户端(运行时) (3)安装plsql. (4)oracle客户端可以不用配置,直接在plsql中数据访问验证
- react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...
- JavaScript 查看函数调用栈
1.调用栈 js中的this与函数调用栈密切相关. this实在函数调用时发生的绑定,它指向完全取决于函数在哪里被调用. 2.示例 <!DOCTYPE html> <html ...
- 修改 Ubuntu 13.04 LAMP 服务器端口号
因为今天想让一台Ubuntu 13.04服务器对外 web 服务的端口号为8000,自己改了一下,但是就是无法访问,端口后依然为 80.所以在网上找了一下修改端口的办法,原来我还少修改了一个文件,这里 ...
- 安装Drupal7.12+Postgresql9.1(Ubuntu Server 12.04)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ OS环境准备 这次是从OS开始安装的.最开始装Ubuntu12.04这里就不再赘述, 唯 ...
- 轻量级代码生成器-OnlyCoder 第一篇
程序猿利器:代码生成器,使用代码生成器已经好几年了,增删改查各种生成,从UI到DATA层均生成过.之前有使用过动软的,T4模板等.... T4生成实体还是没有问题的,但是生成MVC视图就有点烦杂了, ...
- IOS推送通知測试工具PushMeBaby
下载了PushMeBaby在xcode5里中不能使用.类库变了.须要加入Carbon.framework库.在引用的地方改成: #include <Carbon/Carbon.h>.程序就 ...
- STM32 通用T2、T3、T4、T5定时器详解
定时器初始化配置 void TIM3_Configuration(void)//1MS { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_DeI ...
- Openlayer4 - 最好最强大的开源地图引擎
Openlayer4 - 最好最强大的开源地图引擎 # githubhttps://github.com/openlayers/openlayers # 官网http://openlayers.org ...