HDU 2896 virus attack (AC)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.