http://acm.hdu.edu.cn/showproblem.php?pid=2896

另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况。

/*--------------------------------------------------------------------------------------*/
// 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;
int tol; struct Trie
{
int next[][],fail[],end[];
int num[];
int root,L;
int cnt;
int newnode()
{
for(int i=;i<;i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
memset(num,,sizeof num);
cnt = ;
}
void insert(char *s)
{
int len = strlen(s);
int now = root;
for(int i=;i<len;i++)
{
if(next[now][s[i]-'!'] == -)
next[now][s[i]-'!'] = newnode();
now = next[now][s[i]-'!'];
}
end[now]++;
num[now] = cnt++;
}
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]);
}
}
}
}
void query(char *s,int no)
{
int len = strlen(s);
int now = root;
int ans = ;
set <int > web; for(int i=;i<len;i++)
{
now = next[now][s[i]-'!'];
int temp = now;
while(temp != root)
{
ans += end[temp];
if(end[temp]) web.insert(num[temp]);
temp = fail[temp];
}
}
if(!web.empty())
{
printf("web %d:",no);
for(set <int>::iterator it=web.begin();it != web.end();it++)
printf(" %d",*it);
printf("\n");
tol++;
}
}
}ac; char buf[]; int main()
{
while(~scanf("%d",&N))
{
ac.init();
tol = ;
for(int i=;i<N;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%d",&M);
for(int i=;i<=M;i++)
{
scanf("%s",buf);
ac.query(buf,i);
}
printf("total: %d\n",tol);
}
}

AC自动机-HDU2896-模板题的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 一个AC自动机的模板题.用的kuangbin的模板,静态建Trie树.可能遇到MLE的情况要转动态建树. ...

  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. AC自动机 (模板)

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

  4. AC自动机及其模板

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

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

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

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

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

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

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

  8. luogu AC自动机(模板)

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

  9. ac自动机俩模板

    ac自动机算法正确性还没有理解,算法导论也看不懂..等懂了回来发算法专题. #include <cstdio> #include <cstring> using namespa ...

随机推荐

  1. es3的语法来模拟es5的bind方法

    // 简单版 Function.prototype.bind = function(context) { var self = this; return function() { self.apply ...

  2. Apache Beam: 下一代的大数据处理标准

    Apache Beam(原名Google DataFlow)是Google在2016年2月份贡献给Apache基金会的Apache孵化项目,被认为是继MapReduce,GFS和BigQuery等之后 ...

  3. C# yield关键词使用

    C#有一个关键词yield,简化遍历操作实现的语法糖. 下面Insus.NET使用例子来说明,就拿昨晚的一篇<从字符串数组中把数字的元素找出来> http://www.cnblogs.co ...

  4. kettle学习笔记——插件的安装与使用

    一.概述 暂略 二.ODPS插件 https://yq.aliyun.com/articles/68911

  5. WebApi 异步请求(HttpClient)

    还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 废话不多说,直接进入正题: 今天公司总部要求各个分公司把短信接口对接上,所谓的 ...

  6. 安卓自动化测试,贺晓聪之uiautomator设备和选择器~Python详解

    1.设备对象 引入uiautomator,获取设备对象<所谓设备对象可理解为:Android模拟器或者真机> 语法:from uiautomator import device as d ...

  7. kill方法

    删除磁盘上的文件. 语法 Kill 路径名 所需的_路径名_参数是一个字符串表达式,指定要删除的一个或多个文件名. _Pathname_可能包括驱动器和目录或文件夹. 例子删除当前路径下的TXT文档 ...

  8. Centos下Nodejs+npm环境-部署记录

    公司的一个项目上线,需要用到Nodejs和npm环境,这里记录下安装过程,方便回看同时供大家参考. 1)yum安装方式(版本比较老点,v5.12.0 + 3.8.6) 需要在centos中添加epel ...

  9. Mysql双主热备+LVS+Keepalived高可用操作记录

    MySQL复制能够保证数据的冗余的同时可以做读写分离来分担系统压力,如果是主主复制还可以很好的避免主节点的单点故障.然而MySQL主主复制存在一些问题无法满足我们的实际需要:未提供统一访问入口来实现负 ...

  10. Nginx入门【转】

    原文地址:http://blog.csdn.net/u012486840/article/details/53098890 1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上 ...