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. Codechef STREDUC Reduce string Trie、bitset、区间DP

    VJ传送门 简化题意:给出一个长度为\(l\)的模板串\(s\)与若干匹配串\(p_i\),每一次你可以选择\(s\)中的一个出现在集合\(\{p_i\}\)中的子串将其消去,其左右分成的两个串拼接在 ...

  2. MVC ActionResult派生类关系图

    态度决定一切,我要改变的不仅仅是技术,还有对待事情的态度! 先上个图: 由上图可知,ActionResult为根节点,其下有很多子节点!下面简单介绍下: MVC中ActionResult是Action ...

  3. CF 859E Desk Disorder

    题目大意:一个经典的游戏:抢椅子.有\(n\)个人以及\(2n\)把椅子.开始时每个人坐在一把椅子上,而且他们每个人都有一个下一步想坐的位置(可以与之前重合).每一个下一次可以在自己现在做的椅子和想坐 ...

  4. bitcoin源码解析 - 交易 Transcation (一)

    比特币中的交易可谓是比特币的最核心部分.比特币由交易产生,而区块就是用来存储交易的.所以,交易是比特币存在的载体,同时也是比特币中最复杂的部分.交易的运作层层相扣,各个部分缺一不可,十分严密,由此体现 ...

  5. Hexo博客搭建以及Next主题美化的经验之谈

    这并不是一篇博客搭建教程.内容主要包含个人对于Hexo博客搭建的心得,Next6.0主题美化的部分建议,以及摘录一些各种用于博客搭建的link. 在博客园3年6个月,确实也学到了很多,博客园也是目前为 ...

  6. PEP8 Python编程规范

    官方文档: https://www.python.org/dev/peps/pep-0008/ ---------------------------------------------------- ...

  7. 树莓派3代b型静态IP设置,和ssh的wlan配置

    https://blog.csdn.net/qq_36305492/article/details/78607557

  8. JS进阶系列之执行上下文

    function test(){ console.log(a);//undefined; var a = 1; } test(); 也许你会遇到过上面这样的面试题,你只知道它考的是变量提升,但是具体的 ...

  9. 可移动的 HelloWorld

    package com.home.test; import java.awt.Color;import java.awt.Cursor;import java.awt.Font;import java ...

  10. jiedui

    源代码:https://github.com/hanzhaoyan/jieduizuoye/tree/master 功能要求: 该程序用图形界面实现下面功能:用计算机产生一个100以内的随机数,游戏者 ...