HDU3065(AC自动机入门题)
病毒侵袭持续中
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9831 Accepted Submission(s): 3457
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=;
const int N=;
int n;
int Hash[];
struct ID{
char ss[];
ID()
{
memset(ss,,sizeof(ss));
}
}words[];
struct Trie{
int next[MAXN][N],fail[MAXN],end[MAXN];
int tot,root,id;
int newnode()
{
for(int i=;i<N;i++)
next[tot][i]=-;
end[tot++]=;
return tot-;
} void init()
{
tot=;
root=newnode();
} void insert(char buf[],int id)
{
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++)
{
int k=buf[i];
if(next[now][k]==-)
next[now][k]=newnode();
now=next[now][k];
}
end[now]=id;
} void build()
{
fail[root]=root;
queue<int> que;
for(int i=;i<N;i++)
if(next[root][i]==-)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
que.push(next[root][i]);
} while(!que.empty())
{
int now=que.front();
que.pop(); for(int i=;i<N;i++)
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
que.push(next[now][i]);
}
}
} void query(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++)
{
now=next[now][buf[i]];
int temp=now;
while(temp!=root)
{
if(end[temp]!=)
Hash[end[temp]]++;
//若每个模式串只在主串中匹配一次则加上 end[temp]=0;
temp=fail[temp];
}
} for(int i=;i<=n;i++)
{
if(Hash[i]!=)
{
printf("%s: %d\n",words[i].ss,Hash[i]);
}
}
}
};
Trie ac;
char buf[];
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(Hash,,sizeof(Hash));
ac.init();
for(int i=;i<=n;i++)
{
scanf("%s",words[i].ss);
ac.insert(words[i].ss,i);
}
ac.build();
scanf("%s",buf);
ac.query(buf);
}
return ;
}
HDU3065(AC自动机入门题)的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...
- 【hdu2222】【poj2945】AC自动机入门题
HDU2222 传送门 题目分析 裸题:注意构建自动机用的是模式串,思想和kmp很类似. code: #include<iostream> #include<cstdio> # ...
- UVALive-4670 AC自动机入门题 求出现次数最多的子串
/** 链接:http://vjudge.net/problem/UVALive-4670 详见lrj训练指南P216 */ #include<bits/stdc++.h> using n ...
- HDU2896(AC自动机入门题)
病毒侵袭 Time Limit:1000MS Memory Limit:32768KB Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...
- HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...
随机推荐
- USB通讯协议
首先要了解USB枚举过程(自己百度) https://blog.csdn.net/MyArrow/article/details/8270029 USB通讯协议 0. 基本概念 一个[传输](控制.批 ...
- allegro设置鼠标滚轮放大缩小
allegro设置鼠标滚轮放大缩小 allegro16版本以增加可以通过鼠标滚轮进行PCB的放大缩小.具体方法如下: 首先在HOME路径下找到PCBENV文件夹,进入该文件夹打开ENV文件. 在ENV ...
- Terminal emulator
http://en.wikipedia.org/wiki/Terminal_emulator Terminal emulator From Wikipedia, the free encycloped ...
- canvas 橡皮擦效果制作
擦除一定数量后全部消失的有用 imageData 方法的 我把代码贴在最下面 <!DOCTYPE html> <html> <head> <meta char ...
- redis缓冲与数据库
redis是基于key-value结构存储的,且数据存放在内存中,相对数据库读写较快. 基于redis的优势,将redis中存放用户数据,用户第一次登录时,将用户数据从数据库存放redis中,也可以将 ...
- GuozhongCrawler系列教程 (1) 三大PageDownloader
GuozhongCrawler QQ群 202568714 教程源代码下载地址:http://pan.baidu.com/s/1pJBmerL GuozhongCrawler内置三大PageDown ...
- php返回HTTP状态码
HTTP协议状态码,调用函数时候只需要将$num赋予一个下表中的已知值就直接会返回状态了.<?PHP /** * HTTP Protocol defined status codes* HTTP ...
- root无权限删除 原因 进程 占用 文件
[root@test opt]# find / | grep gitlab | xargs rm -rfrm: cannot remove ‘/sys/fs/cgroup/devices/system ...
- Javascript学习之正则表达式详解
什么是正则表达式(regular expreSSion) 正则表达式是一个描述字符模式的对象. 可以处理更复杂的字符串 JavaScript中的正则表达式使用RegExp对象表示 正则表达式用于 ...
- virtualbox Units specified don't exist. SHSUCDX can't install.
version infomatin: virtual box: 5.1.12 platform: win10 x64 target OS: win7 x64 问题 在win10系统上,使用virtua ...