I - 病毒侵袭
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
Input
第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
Output
依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
Sample Input
3
aaa
bbb
ccc
2
aaabbbccc
bbaacc
Sample Output
web 1: 1 2 3
total: 1
思路:
AC自动机的模板题
注意:
这道题我MLE两天 ,拜读了无数人的代码,终于发现我是清零(init())那错了,要在插入字典树那里一步一步清零。
HDU MLE了2页 ?
代码:
|
数组:
time(ms)390 mem(mb) 32.5
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxm=500005;
int n,m,cou,rt;
char s[10005];
int ans[5]= {0};
int ne[maxm][128];
int fail[maxm]; //shi
int vis[maxm]; //zhi
int book[maxm]; //biao
int tot; //ge
void init()
{
mem(ne[0],-1);
fail[0]=-1;
vis[0]=0;
tot=1;
}
void Insert(char *str,int y)
{
int ls=strlen(str);
rt=0;
for(int i=0; i<ls; i++)
{
int x=str[i];
if(ne[rt][x]==-1)
{
mem(ne[tot],-1);
fail[tot]=-1;
ne[rt][x]=tot++;
}
rt=ne[rt][x];
}
vis[rt]=y;
}
void Build()
{
queue<int>qu;
qu.push(0);
while(!qu.empty())
{
int u=qu.front();
qu.pop();
for(int i=0; i<128; i++)
{
if(ne[u][i]!=-1)
{
if(u==0)
fail[ne[u][i]]=0;
else
{
int v=fail[u];
while(v!=-1)
{
if(ne[v][i]!=-1)
{
fail[ne[u][i]]=ne[v][i];
break;
}
v=fail[v];
}
if(v==-1)
fail[ne[u][i]]=0;
}
qu.push(ne[u][i]);
}
}
}
}
void match(char *str)
{
int ls=strlen(str);
rt=0;
cou=0;
mem(book,0);
for(int i=0; i<ls; i++)
{
int x=str[i];
while(ne[rt][x]==-1&&rt!=0)
rt=fail[rt];
rt=ne[rt][x];
if(rt==-1)
rt=0;
int tmp=rt;
while(tmp!=0&&book[tmp]==0)
{
if(vis[tmp])
{
ans[cou++]=vis[tmp];
if(cou==3)
break;
}
book[tmp]=1;
tmp=fail[tmp];
}
if(cou==3)
break;
}
}
int main()
{
while(~scanf("%d",&n))
{
int total=0;
init();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
Insert(s,i);
}
Build();
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%s",s);
match(s);
if(cou)
{
total++;
printf("web %d:",i);
sort(ans,ans+cou);
for(int i = 0; i < cou; i++) {
printf(" %d",ans[i]);
}
printf("\n");
}
}
printf("total: %d\n",total);
}
return 0;
}
结构体数组:
Time Mem
(ms) 265 (MB) 30.9
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=10005;
const int maxm=100005;
int book[maxm];
int ans[5];
int cou=0;
struct Aho
{
int ne[maxm][128]; //
int fail[maxm]; //失配指针
int cnt[maxm];
int vis[maxm];
int tot; //结点个数
queue<int> qu;
void init() //初始化
{
while(!qu.empty())
qu.pop();
mem(ne[0], 0);//
fail[0]=-1;
tot=1;
}
void Insert(char *str,int y) //插入字符串
{
int ls=strlen(str);
int rt=0;
for(int i=0; i<ls; i++)
{
int x=str[i]; //把字符串的字符存入字典树
if(!ne[rt][x])
{
mem(ne[tot],0);
fail[tot]=-1;
ne[rt][x]=tot++; //若这一字符没有在这一分支 ,节点数+1;
}
rt=ne[rt][x];
}
vis[rt]=y;
// printf("stateT_cnt==%d\n",stateT[rt].cnt);
}
void Build() //失配指针,若失配要找到它的父亲,进行宽搜(queue)
{
fail[0]=-1; //根节点指向-1
qu.push(0);
while(!qu.empty())
{
int u=qu.front();
qu.pop();
for(int i=0; i<128; i++) //提出一个点
{
if(ne[u][i]) //如果有
{
if(u==0) //当它是根节点时指向0;
fail[ne[u][i]]=0;
else //没有,就找祖先
{
int v=fail[u];
while(v!=-1) //一直找 直到祖先有i这条边
{
if(ne[v][i])
{
fail[ne[u][i]]=ne[v][i];
break; //保证找到的是最长的
}
v=fail[v];
}
if(v==-1) //没找到
fail[ne[u][i]]=0;
}
qu.push(ne[u][i]);
}
}
}
}
void Match(char *str)
{
int ls=strlen(str);
int rt=0;
cou=0;
mem(book,0);
for(int i=0; i<ls; i++) //找的是以i为后缀有几个匹配的字符串
{
int x=str[i];
if(ne[rt][x])
rt=ne[rt][x];
else //找祖先
{
int f=fail[rt];
while(f!=-1 && ne[f][x]==0) //所指数没有值
f=fail[f];
if(f==-1) //始终没找到,回到根节点
rt=0;
else
rt=ne[f][x];
}
int lin=rt;
while(lin!=0&&book[lin]==0)
{
if(vis[lin])
{
ans[cou++]=vis[lin];
if(cou==3)
break;
}
book[lin]=1;
lin=fail[lin];
}
if(cou==3)
break;
}
}
}aho;
int n;
char S[maxn];
int main()
{
while(~scanf("%d",&n))
{
int total=0;
aho.init(); //初始化
for(int i=1; i<=n; i++)
{
scanf("%s",S);
aho.Insert(S,i); //插入字典树
}
aho.Build(); //建AC自动机的树
int m;
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%s",S);
aho.Match(S);
if(cou)
{
total++;
printf("web %d:",i);
sort(ans,ans+cou);
for(int i = 0; i < cou; i++) {
printf(" %d",ans[i]);
}
printf("\n");
}
}
printf("total: %d\n",total);
}
return 0;
}