AC自动机-HDU3065-简单题
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-简单题的更多相关文章
- 【模版】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...
- 模板】AC自动机(简单版)
模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P3808 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保 ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- AC自动机-HDU2896-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2896 另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况. /*------------------- ...
- AC自动机-HDU2222-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2222 一个AC自动机的模板题.用的kuangbin的模板,静态建Trie树.可能遇到MLE的情况要转动态建树. ...
- 洛谷 P3808 【模板】AC自动机(简单版)
传送门:https://www.luogu.org/problem/P3808 题解:是一个AC自动机的裸题了,注释加在代码里面了 #include<bits/stdc++.h> usin ...
- 【刷题】洛谷 P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- 洛谷P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- luogu P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
随机推荐
- 高阶函数 - aop面向切面编程
Function.prototype.before = function(beforefn) { var __self = this; return function() { beforefn.app ...
- SkylineGlobe 如何二次开发获取三维模型的BBOX和设置Tint属性
测试模型类型选择TerrainModel和Feature两种,测试代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- https原理简析
[转]http://www.cnblogs.com/carsonzhu/p/5225778.html HTTPS的工作原理 HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手 ...
- Codeforces round 1083
Div1 526 这个E考试的时候没调出来真的是耻辱.jpg A 求个直径就完事 #include<cstdio> #include<algorithm> #include&l ...
- [WPF] How to bind to data when the datacontext is not inherited
原文:[WPF] How to bind to data when the datacontext is not inherited 原文地址:http://www.thomaslevesque.co ...
- 在WPF中使用FontAwesome图标字体
原文:在WPF中使用FontAwesome图标字体 版权声明:原创内容转载必须注明出处,否则追究相关责任. https://blog.csdn.net/qq_36663276/article/deta ...
- odoo在底部显示指定字段合计和汇总时显示合计
1.odoo的tree视图底部显示合计 tree 视图,底部显示指定字段合计数 ,视图中字段定义上在sum,取自sale.view_order_tree 销售订单 tree 视图 <field ...
- 【转】Raft 为什么是更易理解的分布式一致性算法
编者按:这是看过的Raft算法博客中比较通俗的一篇了,讲解问题的角度比较新奇,图文并茂,值得一看.原文链接:Raft 为什么是更易理解的分布式一致性算法 一致性问题可以算是分布式领域的一个圣殿级问题了 ...
- [2019校招] - Java多线程面试题总结
Object 的 wait()和notify() 方法 下图为线程状态的图: Object 对象中的 wait()和notify()是用来实现实现等待 / 通知模式.其中等待状态和阻塞状态是不同的.等 ...
- Object-Oriented(二)原型对象
自用备忘笔记 1. 理解原型对象 只要创建函数,函数上就会创建一个 prototype 属性指向函数的原型对象. function Person() {} Person.prototype //指向该 ...