Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2896
This question has been wa for many times, about 8 or 9 times. From CF to half, we started to adjust it here. After unremitting efforts, we finally found that the memset of TMD and VIS was misplaced !!! What's the problem? I was so depressed by this mistake...
Code:
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;
Const int kind = 130;
Struct node {
Node * fail;
Node * Next [kind];
Int count;
Int ID;
Node (){
Fail = NULL;
Count = 0;
Memset (next, null, sizeof (next ));
}
} * Q [2, 500001];
Char keyword [201];
Char STR [10001];
Int head, tail, ID;
Bool vis [501];
Void insert (char * STR, node * root ){
Node * P = root;
Int I = 0, index;
While (STR [I]) {
Index = STR [I];
If (p-> next [Index] = NULL) P-> next [Index] = new node ();
P = p-> next [Index];
I ++;
}
P-> count ++;
P-> id = ID ++;
}
Void build_ac_automation (node * root ){
Int I;
Root-> fail = NULL;
Q [head ++] = root;
While (Head! = Tail ){
Node * temp = Q [tail ++];
Node * P = NULL;
For (I = 0; I <130; I ++ ){
If (temp-> next [I]! = NULL ){
If (temp = root) temp-> next [I]-> fail = root;
Else {
P = temp-> fail;
While (P! = NULL ){
If (p-> next [I]! = NULL ){
Temp-> next [I]-> fail = p-> next [I];
Break;
}
P = p-> fail;
}
If (P = NULL) temp-> next [I]-> fail = root;
}
Q [head ++] = temp-> next [I];
}
}
}
}
Int query (node * root ){
Int I = 0, CNT = 0, index;
Node * P = root;
While (STR [I]) {
Index = STR [I];
While (p-> next [Index] = NULL & P! = Root)
P = p-> fail;
P = p-> next [Index];
P = (P = NULL )? Root: P;
Node * temp = P;
While (temp! = Root & temp-> COUNT = 1 ){
CNT + = temp-> count;
Vis [temp-> id] = true;
Temp = temp-> fail;
}
I ++;
}
Return CNT;
}
Int main (){
Int n, m, sum = 0;
Id = 1;
Scanf ("% d", & N );
Head = tail = 0;
Node * root = new node ();
For (INT I = 0; I <n; I ++ ){
Getchar ();
Scanf ("% s", keyword );
Insert (keyword, root );
}
Scanf ("% d", & M );
Build_ac_automation (Root );
For (INT I = 1; I <= m; I ++ ){
Memset (VIS, false, sizeof (VIS ));
Getchar ();
Scanf ("% s", STR );
If (query (Root )){
Sum ++;
Printf ("Web % d:", I );
For (Int J = 1; j <= N; j ++)
If (vis [J]) printf ("% d", J );
Printf ("\ n ");
}
}
Printf ("Total: % d \ n", sum );
Return 0 ;}