POJ2222 Keywords Search AC自动机模板
#include<cstdio>
#include<cstring>
const int maxn=;
const double eps=1e-;
const long long modn=;
int n;
char a[]={};
char b[*maxn]={};
struct trie{
int next[];
bool exist;
int count;
int fail;
}e[maxn*]={};
int tot,ans;
int q[maxn*]={}; void init(int x,int k,int j){
if(j>k){
e[x].exist=;
e[x].count++;
return;
}
int z=a[j]-'a';
if(!e[x].next[z])
e[x].next[z]=++tot;
init(e[x].next[z],k,j+);
}
void fir(){
memset(e,,sizeof(e)); memset(q,,sizeof(q));
tot=ans=;
}
void doit(){
int head=,tail=,x,y,f;
q[]=;
while(head<=tail){
x=q[head++];
for(int i=;i<;i++){
if(e[x].next[i]){
y=e[x].next[i];
if(x!=){
f=e[x].fail;
while((!e[f].next[i]) && f )
f=e[f].fail;
e[y].fail=e[f].next[i];
}q[++tail]=y;
}
}
}
}
void find(){
scanf("%s",&b);
int k=std::strlen(b);
int x=,z,y;
for(int i=;i<k;i++){
z=b[i]-'a';
while( (!e[x].next[z])&& x){
x=e[x].fail;
}x=e[x].next[z]; y=x;
while(y&&e[y].count){
ans+=e[y].count;
e[y].count=;
y=e[y].fail;
}
}
}
int main(){
int T;scanf("%d",&T);
while(T-->){
fir();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",&a);
init(,std::strlen(a)-,);
}
doit();
find();
printf("%d\n",ans);
}
return ;
}
更新:
整理模板的时候发现自己原来的代码有问题。这个新版本应该没问题了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
int n,siz;
char ch[]={};
char str[maxn*]={};
struct trie{
int sig[];
int cnt;
int vis;
int fail;
}t[maxn*];int tot=;
int q[maxn*]={};int head=,tail=;
void fir(){
tot=;memset(t,,sizeof(t));//memset(q,0,sizeof(q));
}
void init(int x,int j){
if(j>siz-){ t[x].cnt++; return; }
int z=ch[j]-'a';
if(!t[x].sig[z])t[x].sig[z]=++tot;
init(t[x].sig[z],j+);
}
void build_fail(){
int head=,tail=,x,y,f;q[]=;
while(head<=tail){
x=q[head++];
for(int i=;i<;i++)
if(t[x].sig[i]){
y=t[x].sig[i];
if(x){
f=t[x].fail;
while((!t[f].sig[i])&&f)
f=t[f].fail;
t[y].fail=t[f].sig[i];
}q[++tail]=y;
}
}
}
int get_num(){//字符串中包含的单词数量,重复的不记录,
//如果单词x在字符串中出现一次而在单词表中有两个则ans+2
//在字符串中出现两次而单词表中有一个则ans+1
int ans=,x=,y,z;
for(int i=;i<siz;i++){
z=str[i]-'a';
while((!t[x].sig[z])&&x)
x=t[x].fail;
x=t[x].sig[z];y=x;
while(y&&(!t[y].vis)){//保证了每个结尾只访问一次
ans+=t[y].cnt;
t[y].vis=;t[y].cnt=;
y=t[y].fail;
}
}
return ans;
}
int main(){
int T;scanf("%d",&T);
while(T-->){
fir();
scanf("%d",&n);
for(int i=;i<=n;i++){scanf("%s",ch);siz=strlen(ch);init(,);}
build_fail();
scanf("%s",str);siz=strlen(str);
printf("%d\n",get_num());
}
return ;
}
new
POJ2222 Keywords Search AC自动机模板的更多相关文章
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 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 ...
- 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 ...
随机推荐
- Java 9 中的 9 个新特性
Java 8 发布三年多之后,java9已经发布了 . 你可能已经听说过 Java 9 的模块系统,但是这个新版本还有许多其它的更新. 这里有九个令人兴奋的新功能将与 Java 9 一起发布. 1 ...
- CodeForces - 996B
Allen wants to enter a fan zone that occupies a round square and has nn entrances. There already is ...
- webgote的例子(2)Sql注入(SearchGET)
Sql注入(Search/GET) 大家好!!! 现如今web服务在我们的网络上遍地都是,各个终端设备成为我们看不见的客户,web服务也成为公司的招牌.80 443为我们展现的视角也是多姿多彩但背后新 ...
- 安全测试===CSRF攻击简介
http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
- python基础===map, reduce, filter的用法
filter的用法: 这还是一个操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函 ...
- java各种链路工具性能监控工具
Zipkin , Instana 和 Jaeger cat链路追踪系统 用于监控spring 的运行情况,比如内存,线程,池等宏观数据 spring boot admin java反编译 jar xv ...
- libuv 一 环境搭建, hello TTY
引言 - 一时心起, libuv linux 搭建 有一天突然想起来想写个动画. 找了一下 ui 库太大. 后面想起以前弄过的 libuv. 但发现 libuv 相关资料也很少. 所以就有了这些内容. ...
- SpringMVC_HelloWorld_03
通过注解的方式实现一个简单的HelloWorld. 源码 一.新建项目 同SpringMVC_HelloWorld_01 不同的是springmvc配置文件的命名和路径,此处为src/springmv ...
- Ubuntu连接多台Ubuntu server的问题
如果您用的是虚拟机上安装的几个Ubuntu server进行IP配置 要注意以下几点: <1>虚拟机上安装完成Ubuntu server 默认的网络连接方式是NAT ,应该改成桥接网卡 ( ...
- Jinja2 及 render_template 的深度用法
是时候开始写个前端了,Flask中默认的模板语言是Jinja2 现在我们来一步一步的学习一下 Jinja2 捎带手把 render_template 中留下的疑问解决一下 首先我们要在后端定义几个字符 ...