#include<iostream> #include<stdio.h> #include<cstring> using namespace std; char a[501][210]; char str[10001]; int num[10001][501]; int next[10010]; void get_next(char* p, int* next) { int i, j; i = 0; j = -1; next[0] = -1; while (i < strlen(p)) { if (j == -1 || p[i] == p[j]) { i++; j++; //next[i] = j; if (p[i] != p[j]) next[i] = j; else next[i] = next[j]; //cout << "next[i] = " << j << endl; } else { j = next[j]; //cout << "i = " << i << " j = " << j << endl; } } } int index_kmp(char* p, char* x) { int i = -1; int j = -1; int len1 = strlen(p); int len2 = strlen(x); int next[100]; get_next(x, next); while(i < len1 && j < len2) { if (j == -1 || p[i] == x[j]) { i++; j++; } else { j = next[j]; //cout << "i = " << i << " j = " << j << endl; } } if (j == strlen(x)) return i - strlen(x); else return -1; } int main() { int T,i,j,sum,falt,n,m,num2; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;++i) scanf("%s",&a[i]); scanf("%d",&m); memset(num,0,sizeof(num)); for(i=1;i<=m;++i) { scanf("%s",str); memset(next,0,sizeof(next)); for(j=1;j<=n;++j) { get_next(a[j], next); if(index_kmp(str,a[j])!=-1) num[i][j]=1; } } sum=0; for(i=1;i<=m;++i) { falt=0; for(j=1;j<=n;++j) if(num[i][j]!=0) { falt=1; sum++; break; } if(falt==1) { printf("web %d: ",i); num2=0; for(j=1;j<=n;++j) { if(num[i][j]!=0) { printf("%d ",j); num2++; } if(num2==3) break; } printf("\n"); } } printf("total: %d\n",sum); } return 0; }