#include<map>
#include<queue>
#include<stdint.h>
#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
#include<iostream>
#pragma comment(linker,"/STACK:1024000000,1024000000")
const int maxn = 1e5;
char str[10010];
int flag[505*2];
int biao;
struct node
{
int next[maxn][130];
int end[maxn];
int fail[maxn];
int root;
int L ;
void init()
{
L = 0 ;
root = newnode();
}
int newnode()
{
for(int i = 0; i<128; i++)
{
next[L][i] = -1;
}
end[L++]=0 ;
return L -1;
}
void insert(int num)
{
int now = root;
int len = strlen(str);
for(int i = 0; i<len; i++)
{
if(next[now][str[i]]==-1)
{
next[now][str[i]] = newnode();
}
now = next[now][str[i]];
}
end[now] = num;
//printf("he");
}
void getfali()
{
queue<int>q;
fail[root] = root;
for(int i = 0 ; i<128; i++)
{
if(next[root][i]==-1)
{
next[root][i] =root;
}
else
{
fail[next[root][i]] = root ;
q.push(next[root][i]);
}
}
while(!q.empty())
{ // printf("hihihi");
int now = q.front();
q.pop();
for(int i = 0 ; i<128; i++)
{
if( next[now][i]==-1)
{
next[now][i] =next[fail[now]][i];
}
else
{
fail[next[now][i]]= next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
void search()
{
int len = strlen(str);
// puts("s");
//puts(str);
int now = root ;
for(int i = 0; i<len; i++)
{
now = next[now][str[i]];
int temp = now ;
while(temp!=root)
{
if(end[temp]>0)
{
// printf("agaag");
flag[end[temp]] = 1;
biao = 1;
}
temp = fail[temp];
}
}
}
};
node ac;
int main()
{
int n,m;
while(scanf("%d",&n)!= EOF)
{ getchar();
ac.init();
for(int i = 1; i<= n; i++)
{
gets(str);
ac.insert( i );
}
ac.getfali();
scanf("%d",&m);
getchar();
// puts("hihi");
int ans = 0;
for(int i = 1; i<= m; i++)
{
memset(flag,0,sizeof(flag));
biao = 0;
gets(str);
// getchar();
ac.search();
if(biao)
{ ans++;
printf("web %d:",i);
int x;
for(x = 1;x<=n;x++)
{
if(flag[x])
{
printf(" %d",x);
// break;
}
}
putchar('\n');
}
}
printf("total: %d\n",ans);
}
return 0 ;
}
hdu 病毒入侵
最新推荐文章于 2020-05-26 10:36:30 发布