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 ...
随机推荐
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...
- CodeForces 451C Predict Outcome of the Game
Predict Outcome of the Game Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- jquery选择器 :first与:first-child区别
一个例子: <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> ...
- struts2--表单标签
struts2的表单标签可分为两类:form标签本身和包装HTML表单元素的其他标签.form标签本身的行为不同于它内部的元素. struts2表单标签包括: form.textfield.passw ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- 【BZOJ】4636: 蒟蒻的数列
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 145 Solved: 71[Submit][Status][Discuss] ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- cdoj 851 方老师与素数 bfs
方老师与素数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- iOS - UIImagePickerController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...
- JSP Filter,GZIP压缩响应流
url:http://hi.baidu.com/xhftx/blog/item/fbc11d3012648711ebc4af59.html 关键词:JSP,Filter,Servlet,GZIP 现在 ...