1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 100010 5 #define MAXL 510 6 #define MAXM 128 7 using namespace std; 8 char str[MAXN]; 9 int size; 10 bool vis[MAXL], flag; 11 struct node { 12 int pos, fail, next[MAXM]; 13 void Init() { 14 pos = fail = 0; 15 memset(next, 0, sizeof(next)); 16 } 17 }; 18 node tree[MAXN]; 19 void Insert(char *s, int pos) { 20 int now, t; 21 for (now = 0; *s; s++) { 22 t = *s; 23 if (!tree[now].next[t]) { 24 tree[++size].Init(); 25 tree[now].next[t] = size; 26 } 27 now = tree[now].next[t]; 28 } 29 tree[now].pos = pos; 30 } 31 void BFS() { 32 int now, i, p; 33 queue<int> q; 34 q.push(0); 35 while (!q.empty()) { 36 now = q.front(); 37 q.pop(); 38 for (i = 0; i < MAXM; i++) { 39 if (tree[now].next[i]) { 40 p = tree[now].next[i]; 41 if (now) 42 tree[p].fail = tree[tree[now].fail].next[i]; 43 q.push(p); 44 } else 45 tree[now].next[i] = tree[tree[now].fail].next[i]; 46 } 47 } 48 } 49 void Match(char *s) { 50 int now, t, p; 51 for (now = 0; *s; s++) { 52 t = *s; 53 now = tree[now].next[t]; 54 for (p = now; p; p = tree[p].fail) { 55 if (tree[p].pos) 56 flag = vis[tree[p].pos] = true; 57 } 58 } 59 } 60 int main() { 61 int n, m, i, j, ans; 62 while (~scanf("%d", &n)) { 63 tree[0].Init(); 64 for (i = size = 0; i < n; i++) { 65 scanf(" %s", str); 66 Insert(str, i + 1); 67 } 68 BFS(); 69 scanf("%d", &m); 70 for (i = ans = 0; i < m; i++) { 71 flag = false; 72 memset(vis, false, sizeof(vis)); 73 scanf(" %s", str); 74 Match(str); 75 if (flag) { 76 ans++; 77 printf("web %d:", i + 1); 78 for (j = 1; j <= n; j++) { 79 if (vis[j]) 80 printf(" %d", j); 81 } 82 putchar('\n'); 83 } 84 } 85 printf("total: %d\n", ans); 86 } 87 return 0; 88 }