题目大意:比2222多输出一些东西
题解:sig别开太大了!!!HDU绝对可以MLE你到飞起。。。注意兜出去的时候判断条件要写好:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar(' ') 9 using namespace std; 10 const int maxn=60000+10,sig=128; 11 struct ACauto{ 12 struct node{node*ch[sig],*fail;int cnt;char x;}AC[maxn],*root;int nodecnt; 13 int id[156],ans[10001];queue<node*>RAM; 14 node*newnode(){ 15 node*t; 16 if(!RAM.empty()) t=RAM.front(); 17 else t=&AC[++nodecnt]; 18 t->fail=root;t->cnt=0; 19 return t; 20 } 21 void init(){ 22 for(int i=0;i<sig;i++) id[i]=i; 23 root=&AC[0];root->cnt=0;root->x='#';nodecnt=0;return; 24 } 25 void insert(char*s,int id){ 26 int len=strlen(s);node*x=root; 27 for(int i=0;i<len;i++){ 28 int c=s[i]; 29 if(!x->ch[c]) x->ch[c]=newnode(); 30 x=x->ch[c];x->x=s[i]; 31 } x->cnt=id;return; 32 } 33 void getfail(){ 34 queue<node*>Q;Q.push(root); 35 while(!Q.empty()){ 36 node*x=Q.front();Q.pop(); 37 for(int c=0;c<sig;c++){ 38 if(x->ch[c]){ 39 node*p=x->fail;while(p&&!p->ch[c])p=p->fail; 40 x->ch[c]->fail=p?p->ch[c]:root;Q.push(x->ch[c]); 41 } else x->ch[c]=x==root?root:x->fail->ch[c]; 42 } 43 } return; 44 } 45 int query(char*s,int id){ 46 int countn=0;node*x=root; 47 for(int i=0;s[i];i++){ 48 int c=s[i];x=x->ch[c]; 49 for(node*p=x;p&&p->cnt;p=p->fail) ans[countn++]=p->cnt; 50 } 51 if(countn){ 52 sort(ans,ans+countn); 53 printf("web %d: %d",id,ans[0]); 54 for(int i=1;i<countn;i++) if(ans[i]!=ans[i-1]) 55 printf(" %d",ans[i]); 56 puts(""); 57 } 58 return countn>0; 59 } 60 }sol; 61 inline int read(){ 62 int x=0,sig=1;char ch=getchar(); 63 while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} 64 while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); 65 return x*=sig; 66 } 67 inline void write(int x){ 68 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 69 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 70 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 71 } 72 char s[205],t[10005]; 73 void init(){ 74 int T,n=read(),m,sum=0;sol.init(); 75 for(int i=1;i<=n;i++){ 76 scanf("%s",s); 77 sol.insert(s,i); 78 } 79 sol.getfail();m=read(); 80 for(int i=1;i<=m;i++){ 81 scanf("%s",&t); 82 sum+=sol.query(t,i); 83 } 84 printf("total: %d ",sum); 85 return; 86 } 87 void work(){ 88 return; 89 } 90 void print(){ 91 return; 92 } 93 int main(){init();work();print();return 0;}