病毒侵袭持续中
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4809 Accepted Submission(s): 1698
Problem Description
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有
着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打
没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
Input
第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。 接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英
文大写字符”。任意两个病毒特征码,不会完全相同。 在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII
码可见字符(不包括回车)。
Output
按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。 病毒特征码: 出现次数 冒号后有一个空格,按病毒特征码的输入顺序进行输出。
Sample Input
3
AA
BB
CC
ooxxCC%dAAAoen....END
Sample Output
AA: 2
CC: 1
Hint
Hit:
题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。
不想说这题了,是多组数据的,题目坑人:
1 #pragma comment(linker, "/STACK:16777216") 2 #include <iostream> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <algorithm> 6 #include <math.h> 7 #include <queue> 8 #include <string.h> 9 using namespace std; 10 #define maxa 27 11 int aa[20]; 12 int tr; 13 int b[1100]; 14 typedef struct abcd 15 { 16 abcd *next[maxa]; 17 int end; 18 abcd *fail; 19 } abcd; 20 abcd *inti() 21 { 22 abcd *t; 23 t=(abcd *)malloc(sizeof(abcd)); 24 for(int i=0; i<maxa; i++) 25 t->next[i]=NULL; 26 t->end=0; 27 t->fail=NULL; 28 return t; 29 } 30 void insert(abcd *t,char z[],int w) 31 { 32 if(*z=='\0') 33 { 34 t->end=w; 35 return; 36 } 37 if(t->next[*z-'A'+1]==NULL) 38 t->next[*z-'A'+1]=inti(); 39 insert(t->next[*z-'A'+1],z+1,w); 40 } 41 void ac(abcd *t) 42 { 43 queue<abcd*>a; 44 while(!a.empty())a.pop(); 45 for(int i=0; i<maxa; i++) 46 { 47 if(t->next[i]!=NULL) 48 { 49 t->next[i]->fail=t; 50 a.push(t->next[i]); 51 } 52 else t->next[i]=t; 53 } 54 abcd *r,*f; 55 while(!a.empty()) 56 { 57 r=a.front(); 58 a.pop(); 59 for(int i=0; i<maxa; i++) 60 { 61 if(r->next[i]) 62 { 63 a.push(r->next[i]); 64 f=r->fail; 65 while(!f->next[i])f=f->fail; 66 r->next[i]->fail=f->next[i]; 67 } 68 } 69 } 70 } 71 void query(abcd *t,char x[]) 72 { 73 abcd *f,*p=t; 74 while(*x) 75 { 76 while(!p->next[*x-'A'+1])p=p->fail; 77 p=p->next[*x-'A'+1]; 78 f=p; 79 while(f!=t) 80 { 81 int i; 82 if(f->end>0){ 83 b[f->end]++; 84 } 85 f=f->fail; 86 } 87 x++; 88 } 89 } 90 void del(abcd *t) 91 { 92 for(int i=0; i<maxa; i++) 93 { 94 if(!t->next[i]) 95 del(t->next[i]); 96 } 97 free(t); 98 } 99 int main() 100 { 101 int n,i,m; 102 while(cin>>n){ 103 memset(b,0,sizeof(b)); 104 abcd *t; 105 t=inti(); 106 char z[1100][60]; 107 108 for(i=1; i<=n; i++) 109 { 110 cin>>z[i]; 111 insert(t,z[i],i); 112 } 113 ac(t); 114 char x[2000005]; 115 int tt=0; 116 char xx; 117 xx=getchar(); 118 xx=getchar(); 119 int fla=1; 120 while(xx!='\n') 121 { 122 if(xx>='A'&&xx<='Z') 123 { 124 x[tt++]=xx; 125 fla=1; 126 } 127 else 128 { 129 if(fla==1) 130 { 131 x[tt++]='A'-1; 132 fla=0; 133 } 134 } 135 xx=getchar(); 136 } 137 x[tt]='\0'; 138 query(t,x); 139 for(i=1;i<=n;i++) 140 { 141 if(b[i]) 142 cout<<z[i]<<':'<<' '<<b[i]<<endl; 143 } 144 del(t); 145 } 146 }