#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 100008;
int sz, ans[8];
int arr[maxn], mark[maxn];
char ch[508][208];
char txt[10008];
struct Node{
int count, fail, next[94];
void init(){
memset(next, 0, sizeof(next));
count = fail = 0;
}
}node[maxn];
void insert(char *s, int id){
int p = 0;
while(*s){
int c = *s++ - ' ';
if(!node[p].next[c]){
node[sz].init();
node[p].next[c] = sz++;
}
p = node[p].next[c];
}
node[p].count = id;
}
void build_ac(){
int head = 0, rear = 0, p, fail;
for(int i = 0; i < 94; i++) if(node[0].next[i]){
arr[rear++] = node[0].next[i];
}
while(head < rear){
int now = arr[head++];
int fail = node[now].fail;
for(int i = 0; i < 94; i++){
if(p = node[now].next[i]){
arr[rear++] = p;
node[p].fail = node[fail].next[i];
}else node[now].next[i] = node[fail].next[i];
}
}
}
int find(char *s){
int p = 0, cnt = 0;
fill(mark, mark + sz, 0);
while(*s){
int c = *s++ - ' ';
p = node[p].next[c];
int tmp = p;
while(tmp && !mark[tmp]){
mark[tmp] = 1;
if(node[tmp].count) ans[++cnt] = node[tmp].count;
tmp = node[tmp].fail;
}
}
return cnt;
}
int main(){
int n, m;
// freopen("hdu 2896.in", "r", stdin);
while(scanf("%d", &n) != EOF){
sz = 1; node[0].init();
for(int i = 1; i <= n; i++){
scanf("%s", ch[i]);
insert(ch[i], i);
}
build_ac();
scanf("%d",&m);
int tot = 0, cnt;
for(int i = 1; i <= m; i++){
scanf("%s", txt);
cnt = find(txt);
if(cnt) {
tot++;
sort(ans + 1, ans + cnt + 1);
printf("web %d: ",i);
for(int j = 1; j < cnt; j++) printf("%d ", ans[j]);
printf("%d\n", ans[cnt]);
}
}
printf("total: %d\n", tot);
}
//system("pause");
}
HDU 2896
最新推荐文章于 2022-02-25 21:57:21 发布