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. Kubernetes 1.10.4 镜像 版本

    1. gcr.io/google-containers/hyperkube:1.10.4 gcr.io/google_containers/pause-amd64:3.0 gcr.io/google_ ...

  2. MFC入门(二)-- 提取输入框的字符串(定时关机的小程序)

    上篇文章已经让我们有了对于MFC最简单直观的认识,但貌似并无太大的交互性可言,而且其实也没有涉及到数据的交互,所以本文通过做一个时间可以调节的定时关机的Demo来演示. MFC入门(一)地址:http ...

  3. 加解密工具类(含keystore导出pfx)

    java代码如下: package sign; import java.io.FileInputStream; import java.io.FileOutputStream; import java ...

  4. 自建mvc5项目里几个类图

    AccoutController.cs AccountViewModels.cs IdentityModel.cs

  5. exec sp_spaceused如何只返回一个结果集(转载)

    问: 我想把每天数据库的大小自动保存到table中但是exec sp_spaceused是返回2个表,执行下面的语句出错,如何解决? drop table db_size go create tabl ...

  6. 【转】Oracle virtual column(虚拟列)

    为什么要使用虚拟列 (1)可以为虚拟列创建索引(Oracle为其创建function index) (2)可以搜集虚拟列的统计信息statistics,为CBO提供一定的采样分析. (3)可以在whe ...

  7. SQL调优日记--并行等待的原理和问题排查

    概述 今天处理项目,客户反应数据库在某个时间段,反应特别慢.需要我们提供一些优化建议. 现象 由于是特定的时间段慢,排查起来就比较方便.直接查看这个时间段数据库的等待情况.查看等待类型发现了大量的CX ...

  8. 懒人小工具:T4生成实体类Model,Insert,Select,Delete以及导出Excel的方法

    由于最近公司在用webform开发ERP,用到大量重复机械的代码,之前写了篇文章,懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法,但是有人觉得这种方法 ...

  9. 由一个“两次请求”引出的Web服务器跨域请求访问问题的解决方案

    http://blog.csdn.net/cnhnnyzhy/article/details/53128179 (4)Access-Control-Max-Age 该字段可选,用来指定本次预检请求的有 ...

  10. Docker网络解决方案 - Calico部署记录

    简单来说,实现docker跨主机容器间通信,常用的第三方网络方案是Flannel,Weave,Calico:Flannel会为每个host分配一个subnet,容器从这个subnet中分配ip,这些i ...