hduoj-----(2896)病毒侵袭(ac自动机)
病毒侵袭
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11909 Accepted Submission(s): 3088
但
网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的
网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t
收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的
网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
aaa
bbb
ccc
2
aaabbbccc
bbaacc
total: 1
/*hdu 2896 Ac-自动机*/
//#define LOCAL
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int res;
struct Trie
{
struct Trie *fail;
struct Trie *child[];
int id;
};
char s1[];
char t1[];
bool vis[]; //用来表示是否已经访问
void _insert(char *s,Trie *root,int id) //构造一下rie树
{
Trie *cur=root,*newcur;
for(int i=;s[i];i++)
{
int idx=s[i]-;
if(cur->child[idx]==NULL)
{
newcur=new Trie;
for(int j=;j<;j++)
newcur->child[j]=NULL;
newcur->fail=NULL;
newcur->id=;
cur->child[idx]=newcur;
}
cur=cur->child[idx];
}
cur->id=id;
}
//构造失败指针
void ac_fail(Trie *root)
{
Trie *cur,*q ;
queue<Trie*>tre;
tre.push(root);
while(!tre.empty())
{
cur=tre.front();
tre.pop();
for(int i=;i<;i++)
{
if(cur->child[i])
{
if(cur==root)
cur->child[i]->fail=root;
else
{
q=cur;
while(q->fail){
if(q->fail->child[i])
{
cur->child[i]->fail=q->fail->child[i];
break;
}
q=q->fail;
}
if(!q->fail)
cur->child[i]->fail=root;
}
tre.push(cur->child[i]);
}
}
}
}
//查询
void solve(char *s,Trie *root,int id)
{
Trie *cur=root,*newcur;
int ans[]={},t=;
for(int i= ; s[i] ;i++ )
{
while(cur->child[s[i]-]==NULL&&cur!=root)
cur=cur->fail;
cur=cur->child[s[i]-];
if(cur==NULL) cur=root;
newcur=cur;
while(newcur!=root&&newcur->id>&&vis[newcur->id]==)
{
ans[t++]=newcur->id;
vis[newcur->id]=; //表示已经访问了
newcur=newcur->fail;
}
}
if(t>)
{
sort(ans,ans+t);
printf("web %d:",id);
for(int i=;i<t;i++)
printf(" %d",ans[i]);
puts("");
res++;
}
}
void del(Trie *root)
{
if(!root) return ;
for(int i=;i<;i++)
if(root->child[i])
del(root->child[i]);
delete root;
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,m,i;
while(scanf("%d",&n)!=EOF)
{
Trie *root=new Trie;
for(i=;i<;i++)
root->child[i]=NULL;
root->fail=NULL;
root->id=;
for(i=;i<=n;i++)
{
scanf("%s",s1);
_insert(s1,root,i);
}
ac_fail(root);
scanf("%d",&m);
res=;
for(int i=;i<=m;i++)
{
scanf("%s",t1);
memset(vis,,sizeof(vis));
solve(t1,root,i);
}
printf("total: %d\n",res);
del(root);
}
return ;
}
hduoj-----(2896)病毒侵袭(ac自动机)的更多相关文章
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2896 病毒侵袭 AC自动机 基础题
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu2896 病毒侵袭 ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- 【HDU2896】病毒侵袭 AC自动机
[HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- HDU2896 病毒侵袭 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-2896 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
随机推荐
- [转]Unity3D:Gizmos画圆(原创)
using UnityEngine; using System; public class HeGizmosCircle : MonoBehaviour { public Transform m_Tr ...
- Python ftplib
http://automationtesting.sinaapp.com/blog/m_ftplib https://docs.python.org/2/library/ftplib.html 概述 ...
- json \u unicode字符串转化 c++
CString GetUStr(const string & str) { std::string showname = str;//\u6211\u7231\u5317\u4eac\u592 ...
- 安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法
安装 SQL Server Management Studio(SQLManagementStudio_x86_CHS)时,检测时不通过,提示重启电脑,我以为她安装了什么心软件没有重启:所以重启了电脑 ...
- CVE-2015-7547
危险漏洞补丁修复通知 漏洞编号 漏洞编号为CVE-2015-7547 漏洞说明: Google安全团队近日发现glibc存在的溢出漏洞. glibc的DNS客户端解析器中存在基于栈的缓冲区溢出漏洞.当 ...
- vim配置php开发环境
1.ctags-用于代码间的跳转 安装 sudo apt-get install ctags 使用 1). 在某个目录下, 建立tags. ctags -R . --执行之后会在当前目录下生成一个ta ...
- html5 drap & drop
小知识点记录一下:onselectstart,onselect 1.onselectstart 该js方法是用来控制盒中内容是否被允许选中 <head> <style> #tm ...
- nodejs学习笔记<三>关于路由(url)
在网站开发中,路由的设置非常关键.nodejs对路由处理封装了一个比较全面的模块. 来认识下url模块 1)在命令行(cmd)可以直接 node —> url 可直接查看url模块的所有方法. ...
- C10K及C100K问题探讨 & 怎么应对大流量大并发
首先开宗明义,离开业务单独讨论并发,都是扯淡. 就像 https://www.zhihu.com/question/20493166/answer/15998053 这里面说的 谈并发必然要谈业务,空 ...
- Make 教程
Make 命令教程 原文作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2015/02/make.html (在原文基础上稍作修改) 代码变成可执行文件,叫做编 ...