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-模板题的更多相关文章

  1. AC自动机-HDU2896-模板题

    http://acm.hdu.edu.cn/showproblem.php?pid=2896 另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况. /*------------------- ...

  2. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  3. [hdu2222]ac自动机(模板)

    题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...

  4. AC自动机 (模板)

    AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ...

  5. AC自动机及其模板

    模板 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ; ; ; ...

  6. AC自动机(模板+例题)

    首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...

  7. HDOJ-3065(AC自动机+每个模板串的出现次数)

    病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是 ...

  8. AC自动机-HDU3065-简单题

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发. /*------------------- ...

  9. luogu AC自动机(模板)

    完全忘了AC自动机怎么写了qwq,更别说AC自动机上DP了. 今天就好好地学习字符串好了qwq 提一下AC自动机的时间复杂度--设n是模式串的个数,m是文本串的长度,l是模式串的平均长度,那么它的时间 ...

随机推荐

  1. [05] Bean的作用域和生命周期

    1.Bean的作用域和初始化时间 之前我们稍微提到过,Spring中管理的Bean,默认都是单例模式,这意味着你多次获取某个对象,得到的都是相同的对象.单例作用域的显性写法是scope属性,如下,这和 ...

  2. 【小程序】页面无法更新tabbar角标属性时

    在小程序论坛上找答案,一同问了,截图如下

  3. FlashWindowEx实现窗口在任务栏闪烁/变化颜色

    原文:FlashWindowEx实现窗口在任务栏闪烁/变化颜色 效果类似QQ收到新的会话消息任务栏颜色变化 附2小段代码: [System.Runtime.InteropServices.DllImp ...

  4. js 稍微判断下浏览器 pc 还是手机

        function isMobile() {    var a=navigator.userAgent;   var ref=/.*(Android|iPhone|SymbianOS|iPad| ...

  5. Linux性能评测工具之一:gprof篇

    这些天自己试着对项目作一些压力测试和性能优化,也对用过的测试工具作一些总结,并把相关的资料作一个汇总,以便以后信手拈来! 1 简介 改进应用程序的性能是一项非常耗时耗力的工作,但是究竟程序中是哪些函数 ...

  6. vue 首页背景图片加载完成前增加 loading 效果 -- 使用 new Image() 实现

    1. 创建 loading 公用组件 <template> <div class="load-container"> <div class=" ...

  7. QQ群管理员申请帖(本次截止日期为2017-03-25)

    本帖专门为技术交流群申请管理员专用. 管理员的权利: 1.有权在成员违规的情况下直接剔除. 2.有权加入多个交流群. 3.有权引人入群. 4.艾特全体是权利,但要慎用,通常情况下,没有我本人的授意,不 ...

  8. Centos6下zookeeper集群部署记录

    ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. Zookeeper设计目的 最终一致性:client不论 ...

  9. C. The Tower is Going Home

    链接 [http://codeforces.com/contest/1075/problem/C] 题意 有个1e9*1e9的棋盘(1,1)位置在左下角也就是车这枚棋子的位置,然后有n个在某一列后面划 ...

  10. Week 2 代码审查

    我的伙伴是6班的小伙子潘礼鹏,经过几天的相处我觉得真的是说话很有趣的人,性格非常好,我们很划得来. 以下为我对他的代码的审查结果: VS2012与VS2013的兼容性 在这里写一个工具集的问题,不同的 ...