标签:des style blog color io java strong ar for
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11422 Accepted Submission(s): 2957
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> using namespace std; struct node{ node *fail; node *son[128]; int num; node() { fail=NULL; for(int i=0;i<128;i++)son[i]=NULL; num=0; } } *que[100101]; node *root; int ans[11]; int ple,xzq,i,j,k,t,m,n,tl; char s[100101]; bool vis[511]; void add(int x) { int i,ts; node *p; p=root; for(i=0;i<n;i++){ ts=s[i]; if(p->son[ts]==NULL)p->son[ts]=new node(); p=p->son[ts]; } p->num=x; } void bfs() { node *p; node *q; int l,r,i,x,j,k; l=r=1; que[1]=root; while(l<=r){ p=que[l]; for(i=0;i<128;i++)if(p->son[i]!=NULL){ r++; que[r]=p->son[i]; if(p==root)p->son[i]->fail=root; else{ q=p->fail; while(q!=NULL){ if(q->son[i]!=NULL){ p->son[i]->fail=q->son[i]; break; } q=q->fail; } if(p->son[i]->fail==NULL)p->son[i]->fail=root; } } l++; } } void work() { int i,ts; node *p; node *q; p=root; for(i=0;i<n;i++){ ts=s[i]; if(p->son[ts]!=NULL)p=p->son[ts]; else{ if(p==root)p=root; else{ q=p->fail; while(q!=NULL){ if(q->son[ts]!=NULL){ p=q->son[ts]; break; } q=q->fail; } if(q==NULL)p=root; } } if(p->num!=0){ vis[p->num]=true; ple++; q=p->fail; while(q!=NULL){ if(q->num!=0)vis[q->num]=true; q=q->fail; } } } } int main() { scanf("%d",&t); root=new node(); for(i=1;i<=t;i++){ scanf("%s",&s); n=strlen(s); add(i); } bfs(); scanf("%d",&m); for(i=1;i<=m;i++){ ple=0; scanf("%s",&s); n=strlen(s); memset(vis,false,sizeof(vis)); work(); if(ple!=0){ xzq++; printf("web %d:",i); for(j=1;j<=t;j++)if(vis[j])printf(" %d",j); printf("\n"); } } printf("total: %d\n",xzq); }
标签:des style blog color io java strong ar for
原文地址:http://www.cnblogs.com/applejxt/p/3954714.html