hdu2896
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int wl = 128;
struct Node
{
Node *fail;
Node *next[wl];
int counts, id;
Node()
{
counts = id = 0;
fail = NULL;
for (int i = 0; i < wl; i++)
{
next[i] = NULL;
}
}
}*q[100000 + 5];
struct Web
{
int num;
int temp[5];
}web[1000 + 10];
char keyword[200 + 10];
char str[10000 + 10];
int head, tail, tot;
void inserts(char *s, Node *root)
{
int i = 0;
Node *p = root;
while (s[i])
{
int t = s[i];
if (p->next[t] == NULL)
{
p->next[t] = new Node();
}
p = p->next[t];
i++;
}
p->counts++;
p->id = tot;
}
void build_ac_automation(Node *root)
{
root->fail = NULL;
q[tail++] = root;
while (head != tail)
{
Node *temp = q[head++];
Node *p = NULL;
for (int i = 0; i < 128; i++)
{
if (temp->next[i] != NULL)
{
if (temp == root)
{
temp->next[i]->fail = root;
}
else
{
p = temp->fail;
while (p != NULL)
{
if (p->next[i] != NULL)
{
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL)
{
temp->next[i]->fail = root;
}
}
q[tail++] = temp->next[i];
}
}
}
}
int query(Node *root)
{
head = tail = 0;
int len = strlen(str), i = 0;
int cnt = 0;
Node *p = root;
bool flag[500 + 10];
memset(flag, false, sizeof(flag));
while (str[i])
{
int t = str[i];
while (p->next[t] == NULL && p != root)
{
p = p->fail;
}
p = p->next[t];
if (p == NULL)
{
p = root;
}
Node *temp = p;
while (temp != root && !flag[temp->id])
{
if (temp->counts)
{
web[tot].num++;
web[tot].temp[cnt++] = temp->id;
flag[temp->id] = true;
}
temp = temp->fail;
}
i++;
}
return cnt;
}
void input()
{
int n, m;
while (scanf("%d", &n) != EOF)
{
tot = 0;
Node *root = new Node();
scanf("%d", &n);
while (n--)
{
gets(keyword);
inserts(keyword, root);
tot++;
}
build_ac_automation(root);
tot = 0;
memset(web, 0, sizeof(web));
scanf("%d", &m);
getchar();
while (m--)
{
gets(str);
query(root);
tot++;
}
int ans = 0;
for (int i = 0; i < tot; i++)
{
if (web[i].num != 0)
{
ans++;
printf("web %d:", i + 1);
sort(web[i].temp, web[i].temp + web[i].num);
for (int j = 0; j < web[i].num; j++)
{
printf(" %d", web[i].temp[j] + 1);
}
printf("\n");
}
}
printf("total: %d\n", ans);
}
}
int main()
{
input();
return 0;
}