AC自动机-HDU2222-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2222
一个AC自动机的模板题。用的kuangbin的模板,静态建Trie树。可能遇到MLE的情况要转动态建树。
AC自动机的讲解看这里
http://blog.csdn.net/niushuai666/article/details/7002823
http://blog.csdn.net/mobius_strip/article/details/22549517
/*--------------------------------------------------------------------------------------*/
// Helica's header
// Second Editions
// 2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std; int N,M,T; struct Trie
{
int next[][],fail[],end[];
int root,L;
int newnode()
{
for(int i=;i<;i++) next[L][i] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
} void insert(char *s)
{
int len = strlen(s);
int now = root;
for(int i=;i<len;i++)
{
if(next[now][s[i]-'a'] == -)
next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
}
end[now]++;
} void build()
{
queue <int> Q;
fail[root] = root;
for(int i=;i<;i++)
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
} while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} } int query(char *s)
{
int len = strlen(s);
int now = root;
int ans = ;
for(int i=;i<len;i++)
{
now = next[now][s[i]-'a'];
int temp = now;
while(temp != root)
{
ans += end[temp];
end[temp] = ;
temp = fail[temp];
}
}
return ans;
}
void debug()
{
for(int i=;i<L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j=;j<;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
}; char buf[];
Trie ac; int main()
{
scanf("%d",&T);
while(T--)
{
ac.init();
scanf("%d",&N);
for(int i=;i<N;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
//ac.debug();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
}
第N次入门AC自动机。。。成功。。。
AC自动机-HDU2222-模板题的更多相关文章
- AC自动机-HDU2896-模板题
		http://acm.hdu.edu.cn/showproblem.php?pid=2896 另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况. /*------------------- ... 
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
		Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ... 
- [hdu2222]ac自动机(模板)
		题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ... 
- AC自动机 (模板)
		AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ... 
- AC自动机及其模板
		模板 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ; ; ; ... 
- AC自动机(模板+例题)
		首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ... 
- HDOJ-3065(AC自动机+每个模板串的出现次数)
		病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是 ... 
- AC自动机-HDU3065-简单题
		http://acm.hdu.edu.cn/showproblem.php?pid=3065 需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发. /*------------------- ... 
- luogu AC自动机(模板)
		完全忘了AC自动机怎么写了qwq,更别说AC自动机上DP了. 今天就好好地学习字符串好了qwq 提一下AC自动机的时间复杂度--设n是模式串的个数,m是文本串的长度,l是模式串的平均长度,那么它的时间 ... 
随机推荐
- http协议进阶(四)报文首部
			之前写的关于报文首部的传送门: 报文首部:http://www.cnblogs.com/imyalost/p/5708445.html 通用首部字段:http://www.cnblogs.com/im ... 
- 你真的会python嘛?
			前言 我这个博客一直都是一些技术分享,show code的地方,我从来没有写过个人生活或者情感杂谈,当然我也从来没有谈论过我对什么东西的喜恶. 很多人喜欢喷XX语言,喜欢谈论XX和YY的优缺,甚至凑了 ... 
- java并发编程CountDownLatch
			/** * CountDownLatch用法 * CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能.比如有一个任务A, * 它要等待其他4 ... 
- linux编程头文件所在路径的问题
			一.问题引入 1.头文件与库 当我们在PC主机linux环境下(如ubuntu),编写linux应用程序,然后利用gcc来编译.在源代码的开始位置会写入头文件,那是因为我们使用了系统提供的库函数,例如 ... 
- ABP+AdminLTE+Bootstrap Table权限管理系统第六节--abp控制器扩展及json封装以及6种处理时间格式化的方法
			返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 一,控制器AbpController 说完了Swagger ui 我们再来说一下abp对控制器的处理和json的封 ... 
- RocketMQ 简单梳理 及 集群部署笔记
			一.RocketMQ 基础知识介绍Apache RocketMQ是阿里开源的一款高性能.高吞吐量.队列模型的消息中间件的分布式消息中间件. 上图是一个典型的消息中间件收发消息的模型,RocketMQ也 ... 
- Vue 回顾之指令(关于input自动聚焦的问题)
			用了Vue也一年多了,虽然对大部分内容都比较熟悉,但有些用法可能会起到意想不到的作用. 今天在做一个关于抽奖的需求,要求是每次点击编辑按钮显示编辑框,要求自动聚焦. 一开始想到了autofocus属性 ... 
- sheet制作返回按钮
			=HYPERLINK("#目录!A1","目录!A1") =HYPERLINK("#"&A2&"!A1" ... 
- Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165
			1.Advantages and disanvantages of Peer Programming advantages The code are constantly validated by t ... 
- M2事后总结
			照片 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "北航"Clubs旨在于解决北航校内社团管理与学生参与社团活动的困难的 ... 
