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 ...
随机推荐
- gdb调试多进程和多线程命令(转)
1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...
- Spring HibernateTemplate
HibernateTemplate利用模板设计模式,可将重复的opensession getcurrentsession工作省去,只将必要操作执行即可,其它的由spring来帮我们处理. < ...
- 关于继承Fragment后重写构造方法而产生的错误
在android开发中.写了一个关于继承Fragment的类时,假设有重载构造函数时.会提示"Avoid non-default constructors in fragments: use ...
- 初学spring(二)
1.spring推荐使用接口编程,配合di可以达到层与层之间解耦
- Lucene4.0 LogMergePolicy
其特点是给定的段列表顺序归并,不像TieredMergePolicy那样按大小排序之后决定. norm = log(10),levelFloor=log(minMergeSize)/norm,对段列表 ...
- Atitit.android jsbridge v1新特性
Atitit.android jsbridge v1新特性 1. Java代码调用js并传参其实是通过WebView的loadUrl方法去调用的.只是参数url的写法不一样而已1 2. 三.JAVA ...
- 复杂可编程逻辑器件CPLD的基本结构
复杂可编程逻辑器件CPLD的基本结构 文章出处:czhlcai 发布时间: 2008/12/08 | 6911 次阅读 专业薄膜开关打样工厂,12小时加急出货 1.基于乘积项的CPLD结构 CPL ...
- 使用SVN管理unityproject
我们的项目使用SVN管理.这几天遇到了几个问题,攻克了一下.顺便做了一个总结. 1.关于使用SVN管理unity项目的一些设置和说明 首先在unity中进行两部操作:Edit->Proje ...
- python模块学习之json
更多信息请参考官网地址: https://docs.python.org/3.6/library/json.html 19.2. json - JSON编码器和解码器 Source code: Lib ...
- Math函数的"四舍五入",Floor,Ceiling,Round的一些注意事项!
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...