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

需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发。

/*--------------------------------------------------------------------------------------*/
// 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,ans[];
char buf[];
map <int,string > m; struct Trie
{
int next[][],fail[],end[],index[];
int root,L,cnt;
int newnode()
{
for(int i=;i<;i++) next[L][i] = -;
end[L++] = ;
return L-;
} void init()
{
L = ;
cnt = ;
memset(index,,sizeof index);
memset(ans,,sizeof ans);
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]++;
index[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 len = strlen(s);
int now = root;
for(int i=;i<len;i++)
{
if(s[i]<'A'||s[i]>'Z')
{
now = root;
continue;
}
now = next[now][s[i]-'A']; int temp = now;
while(temp != root)
{
ans[index[temp]] += end[temp];
temp = fail[temp];
}
}
} 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");
}
}
}ac; int main()
{
while(~scanf("%d",&N))
{
ac.init();
m.clear(); for(int i=;i<N;i++)
{
scanf("%s",buf);
m.insert(pair<int,string>(i,string(buf)));
ac.insert(buf);
}
getchar();
ac.build();
scanf("%s",buf);
ac.query(buf); for(int i=;i<N;i++) if(ans[i])
{
printf("%s: %d\n",m[i].c_str(),ans[i]);
}
}
}

AC自动机-HDU3065-简单题的更多相关文章

  1. 【模版】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...

  2. 模板】AC自动机(简单版)

    模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P3808 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保 ...

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

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

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

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

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

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

  6. 洛谷 P3808 【模板】AC自动机(简单版)

    传送门:https://www.luogu.org/problem/P3808 题解:是一个AC自动机的裸题了,注释加在代码里面了 #include<bits/stdc++.h> usin ...

  7. 【刷题】洛谷 P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  8. 洛谷P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  9. P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  10. luogu P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

随机推荐

  1. 《Google软件测试之道》测试工程师

    愿和我一样读过这本书的人有所共鸣或者启发,愿没读过这本书的人,能获得一点点收获... 说到软件测试工程师,首先我们需要明白一个问题,软件测试工程师的职责是什么? 关于这个话题,不同的人有不同的定义:抛 ...

  2. 如何修改Oracle服务IP地址

    oracle数据库所在的机器更改IP地址后,发现无法连接,解决这个问题,需要修改一下对应的文件: F:\app\zhaohe\product\11.2.0\dbhome_1\NETWORK\ADMIN ...

  3. 源码篇:Python 实战案例----银行系统

    import time import random import pickle import os class Card(object): def __init__(self, cardId, car ...

  4. CF1105E Helping Hiasat 最大团

    传送门 发现自己不会求最大团了可海星 如果将每一个朋友看做点,将两个\(1\)之间存在\(2\)操作的所有朋友之间互相连边,那么我们最后要求的就是这个图的最大独立集. 某个图的最大独立集就是反图的最大 ...

  5. 使用awk按照行数切割文件

    最近在做一个事情,需要将一个文本文件按照行数进行切割,然后用了,awk的方法,感觉很好用, 记录一下. 脚本如下: #!/bin/bash ## 文件效果: 根据行数来切割文件 ## 参数1为要切割的 ...

  6. C# 时间戳与DateTime互转

    #region 转换时间为unix时间戳 /// <summary> /// 转换时间为unix时间戳 /// </summary> /// <param name=&q ...

  7. Linux文件下载(转)

    wget是Linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如: # wget http://www.linuxsense.org/xxxx/xxx. ...

  8. mysql 通过慢查询日志查写得慢的sql语句

    MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow-queries[=file_name]选项启动时,mysqld 会写一个包含所有执行时间超过long_query_t ...

  9. 快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求

    本文快速分享一下快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求的方法,供大家参考. 原文发表于我的技术博客 零配置方案 最新的苹果审核政策对 API 的 IPv6 以及 ...

  10. Jmeter-使用Ultimate Thread Group插件来设置负载场景

    前言: Jmeter插件相关请移步:https://www.jianshu.com/p/130c7fddeddf 自定义线程组:jp@gc - Ultimate Thread Group,功能强大,可 ...