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,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
随机推荐
- SkylineGlobe MFC C++ 开发示例代码
SkylineGlobe的SDK底层是跨平台的C++内核,面向不同平台封装原生的API,具有很高的执行效率, 下面是C++二次开发时的示例代码: #import "D:\Program Fi ...
- Oracle 在函数或存储过程中执行一条插入语句并返回主键ID值
有时,我们需要往一张表插入一条记录,同时返回主键ID值. 假定主键ID的值都是通过对应表的SEQUENCE来获得,然后进行ID赋值 这里有几种情况需要注意: 1)如果建表语句含有主键ID的触发器,通过 ...
- 自建mvc5项目里几个类图
AccoutController.cs AccountViewModels.cs IdentityModel.cs
- Vue-条件渲染v-if与v-show
一.共同点 根据数据值来判断是否显示DOM元素 二.区别 代码: <!DOCTYPE html> <html lang="en"> <head> ...
- C#实现一张塔松叶
前段时间,Insus.NET有实现一组字符串在输出时,靠左或靠右对齐.<输出的字符靠右对齐>http://www.cnblogs.com/insus/p/7953304.html 现在In ...
- sun.misc.BASE64Decoder 限制取消
sun.misc.BASE64Decoder Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -&g ...
- 2016年总结 - Java程序员
一 . 技术积累 (1)代码规范 1.1.1.通常的模块分布:一般如果你要实现一个web应用,你从后台将数据展示到前端页面,在一个比较大的公司,你少不了跟其他项目有交集(你调用他的接口,他依赖你的接口 ...
- ExtJS框架基础:事件模型及其常用功能
前言 工作中用ExtJS有一段时间了,Ext丰富的UI组件大大的提高了开发B/S应用的效率.虽然近期工作中天天都用到ExtJS,但很少对ExtJS框架原理性的东西进行过深入学习,这两天花了些时间学习了 ...
- ABAQUS粘弹性边界及地震荷载施加的简单实现(Matlab生成input文件)
思路 粘弹性边界因为能够考虑地基辐射阻尼而使得结构抗震的计算结果更趋于合理,所以在需要考虑结构地基相互作用的结构抗震计算时,是较为常用的地基边界处理和地震荷载施加方法.而ABAQUS软件是经常用来进行 ...
- json中获取key值
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...