HDU2896(AC自动机入门题)
Time Limit:1000MS Memory Limit:32768KB
Description
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
Input
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20―200之间。
每个病毒都有一个编号,依此为1―N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000―10000之间。
每个网站都有一个编号,依此为1―M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
Output
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=;
const int N=;
int res;
struct Trie{
int next[MAXN][N],fail[MAXN],end[MAXN];
int root;
int tot,id;
int newnode()
{
for(int i=;i<N;i++)
next[tot][i]=-;
end[tot++]=;
return tot-;
} void init()
{
tot=;
id=;
root=newnode();
} void insert(char buf[])
{
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()
{
queue<int> que;
fail[root]=root;
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 t)
{
int len=strlen(buf);
int num[],cnt=;
int now=root;
for(int i=;i<len;i++)
{
now=next[now][buf[i]];
int temp=now;
while(temp!=root)
{
if(end[temp]!=)
num[cnt++]=end[temp];
//end[temp]=0; 有多个主串参与匹配
temp=fail[temp];
}
}
if(cnt==) return ;
sort(num,num+cnt);
cnt=unique(num,num+cnt)-num;//去重
printf("web %d:",t);
for(int i=;i<cnt;i++)
printf(" %d",num[i]);
printf("\n");
res++;
}
};
int n,m;
Trie ac;
char buf[];
int main()
{
while(scanf("%d",&n)!=EOF)
{
res=;
ac.init();
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",res);
} return ;
}
HDU2896(AC自动机入门题)的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- 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 ...
- HDU3065(AC自动机入门题)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...
随机推荐
- Unity Shaders and Effects Cookbook (3-4) 使用高光贴图
在学习完上一节之后.已经了解了在Unity 中怎样实现一个高光 Shader ,可是会有一个问题.就是效果看起来不切实际,如以下的问题 我用一张图片贴到了Cube上面.然后用了一个高光材质,得到了下图 ...
- 百科知识 .e,.ec文件如何打开
1 .e是易语言源文件,你可以从以下网址下载e语言编程环境: http://www.xiazaiba.com/html/409.html 2 安装之后会自动关联.e文件. 3 打开一个e语言文 ...
- STP 根桥、根port、指定port是怎样选举的
学习CCNA过程中,对交换机的根桥.跟port以及指定port选举有些迷糊.也度娘了一番,总认为一部分人解释的不够全面精细.通过细致研究终于有了自己的理解,分享给大家,假设纰漏,欢迎指正. STP收敛 ...
- valid-palindrome——判断带符号数字字母的字符串是否为回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...
- jackrabbit官方英文文档加补充(转载)
关于Jackrabbit To get started with Jackrabbit you should first become familiar with the JCR API. Downl ...
- ContentPresenter理解
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...
- 零基础学python-2.18 异常
这一节说一下异常except 继续沿用上一节的代码.我有益把文件名称字搞错.然后在结尾部分加上异常捕捉: try: handler=open("12.txt")#在这里我特别将文件 ...
- POJ 1195 Mobile phones (二维树状数组)
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- js怎么限制文本框input只能输入数字
1.说明 本篇文章介绍怎么使用js限制文本框只能输入数字 2.HTML代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1 ...