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 ...
随机推荐
- 64位系统注冊32位的directshow filter文件
在SERVER2008上注冊自己写的directshow filter 的dll或者ax文件的时候总是提示 [Window Title] RegSvr32 [Content] 模块".\ba ...
- Python学习笔记(四)多进程的使用
python中多进程与Linux 下的C基本相同. fork的基本使用 先看最简单的例子: # coding: utf-8 import os def my_fork(): pid = os. ...
- Linux——获取IP及其中发现的问题
由于最近在学习网络编程,喜欢玩IP地址. 首先,我需要搞清楚如何获得本机IP. ===========================================11.04============ ...
- lodash 集合处理方法 map和filter区别
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 单用户模式下mount -o remount,rw / 有大用途
我们的Linux系统在无法启动时候,通常需要进入单用户模式下进行修改一些配置文件,或调整一些参数方可.但是在进入单用户模式后,我们的/文件系统是只读模式,无法进行修改,那么这个时候我们就需要用到一条命 ...
- sqlserver,获取调用存储过程返回数据的方法。
1,获取存储过程最后select返回的结果集.SELECT 数据集返回值. 因为select返回的结果是一个表.所以返回的结果需要用一个表接收.使用临时表接收. 被调用的存储过程最后是这样:返回了一个 ...
- 使用python-nmap 搭建基本端口扫描器
代码地址如下:http://www.demodashi.com/demo/13255.html 一.前言 注意: 本文相关教程仅供个人学习使用,切勿用于非法用途,否则造成的相关损失及影响,作者不承担任 ...
- Jenkins插件开发资料
原文地址:http://www.ciandcd.com/?p=181 Jenkins plugin 开发: Document http://hudson-ci.org/docs/index.html ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\
解决方法: 1:设置 C:\windows\temp 文件夹安全权限 添加用户 NETWORK SERVICE 写入和读取权限 2:设置 C:\windows\temp 文件夹安全权限 添加用户 ...
- rocketmq发送消息代码
DefaultMQProducer defaultMQProducer = new DefaultMQProducer(); defaultMQProducer.setProducerGroup(Co ...