3 aaa bbb ccc 2 aaabbbccc bbaacc
web 1: 1 2 3 total: 1
gaojie | We have carefully selected several similar problems for you: 2243 2825 3341 3247 2296
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> using namespace std; int m,n,ans,ch[100000][130]; int fail[100000],tot=0,is_end[100000],num[503],vis[100000]; char s[10003]; void insert(char *s,int k) { int len=strlen(s); int now=0; for (int i=0;i<len;i++) { int x=s[i]; if (!ch[now][x]) ch[now][x]=++tot; now=ch[now][x]; } is_end[now]=k; } void build() { queue<int> p; int now=0; for (int i=0;i<=128;i++) if (ch[now][i]) p.push(ch[now][i]); while (!p.empty()) { now=p.front(); p.pop(); for (int i=0;i<=128;i++) { if (!ch[now][i]) { ch[now][i]=ch[fail[now]][i]; continue; } int x=ch[now][i]; fail[x]=ch[fail[now]][i]; p.push(x); } } } void solve(char *s,int k) { int len=strlen(s); int now=0; memset(num,0,sizeof(num)); memset(vis,0,sizeof(vis)); for (int i=0;i<len;i++) { vis[now]=1; int x=s[i]; int m=ch[now][x]; while (m!=0&&!vis[m]) { if (is_end[m]) num[0]++,num[num[0]]=is_end[m]; vis[m]=1; m=fail[m]; } now=ch[now][x]; } if (num[0]) { sort(num+1,num+num[0]+1); printf("web %d:",k); for (int i=1;i<=num[0];i++) printf(" %d",num[i]); printf("\n"); ans++; } } int main() { scanf("%d\n",&n); for (int i=1;i<=n;i++) { gets(s); insert(s,i); } build(); scanf("%d\n",&m); for (int i=1;i<=m;i++) { gets(s); solve(s,i); } printf("total: %d\n",ans); return 0; }