Basic questions about the attack of hdu 2896 virus on AC automatic machine

Source: Internet
Author: User

Virus attacks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 7381 Accepted Submission (s): 1935

 

Problem Description
When the light of the sun gradually becomes invisible to the Moon and the world loses its light, the earth will usher in the darkest moment .... At this moment, people are excited-how happy we can see the wonders of the world we met in 500 in our lifetime ~~
However, there are always some websites on the Internet. With the curiosity of the people, the website began to spread viruses under the banner of introducing the eclipse. Little t unfortunately became one of the victims. So angry, he decided to find out all the websites with viruses in the world. Of course, everyone knows that this is impossible. Mr. t insisted on completing the task that he could not do. He said, "Son, son, sun, and sun are also helpless !" (Another employee has been born after yugong ).
Everything is hard at the beginning. Mr. t has collected a lot of virus signatures and the source code of a batch of strange websites. He wants to know which websites contain viruses, what virus does it carry? By the way, I want to know how many websites with viruses he has collected. At this time, he did not know how to start. So please help me. T is an acute problem, so the sooner the problem is solved, the better ~~

 


Input
The first line, an integer N (1 <=n <= 500), indicates the number of virus signatures.
In the next N rows, each line indicates a virus pattern, and the character string length is between 20 and.
Each virus has a number, which is 1-n.
Virus signatures of different numbers are not the same.
In the next row, there is an integer M (1 <= M <= 1000), indicating the number of websites.
In the next M line, each line represents a website source code, and the source code string length is between and.
Each website has a ID (1-m.
The preceding strings contain both printable ASCII characters (excluding carriage return ).

 


Output
Output by website number in ascending order in the following format: the website number with viruses and the number containing viruses. Each line contains the information of a website containing viruses.
Web Site No.: virus no., virus No...
There is a space after the colon, and the virus numbers are arranged in ascending order. Two virus numbers are separated by a space. If a website contains viruses, the number of viruses cannot exceed 3.
The last line outputs statistics in the following format:
Total: number of websites with viruses
There is a space after the colon.

 


Sample Input
3
Aaa
Bbb
Ccc
2
Aaabbbccc
Bbaacc


Sample Output
Web 1: 1 2 3
Total: 1


Source
2009 Multi-University Training Contest 10-Host by NIT
 


Recommend
Gaojie
 
 
This is basically a template question. You just need to modify the query () function so that the while loop in it does not mark the vertices that have passed.
 
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <malloc. h>
# Include <queue>
Using namespace std;
Int ans [1111] [5];
Char str [1111111];
# Define HTTP 130
Int id, vis [1111], a [1111];
Struct node
{
Int count;
Struct node * next [cc];
Struct node * fail;
Void init ()
{
Int I;
For (I = 0; I <cc; I ++)
Next [I] = NULL;
Count = 0;
Fail = NULL;
}
} * Root;
Void insert ()
{
Int len, k;
Node * p = root;
Len = strlen (str );
For (k = 0; k <len; k ++)
{
Int pos = str [k]-31;
If (p-> next [pos] = NULL)
{
P-> next [pos] = new node;
P-> next [pos]-> init ();
P = p-> next [pos];
}
Else p = p-> next [pos];
}
P-> count = id;
}
 
Void getfail ()
{
Int I;
Node * p, * temp, * son;
Queue <struct node *> que;
Que. push (root );
P = root;
While (! Que. empty ())
{
Temp = que. front ();
Que. pop ();
For (I = 0; I <cc; I ++)
{
Son = temp-> next [I];
If (son! = NULL)
{
If (temp = root)
{
Son-> fail = root;
}
Else
{
P = temp-> fail;
While (p)
{
If (p-> next [I]! = NULL)
{
Son-> fail = p-> next [I]; //
Break;
}
// Else note that there is no else
P = p-> fail;
}
If (p = NULL) son-> fail = root;
}
Que. push (son );
}
}
}
}
 
Void query ()
{
Int len, I, cnt = 0, pos;
Len = strlen (str );
Node * p, * temp;
P = root;
For (I = 0; I <len; I ++)
{
Pos = str [I]-31;
While (! P-> next [pos] & p! = Root) p = p-> fail;
P = p-> next [pos];
If (! P) p = root;
Temp = p;
While (temp! = Root) // you only need to modify it here.
{
If (temp-> count> = 1 &&! Vis [temp-> count])
{
Ans [id] [cnt ++] = temp-> count;
Vis [temp-> count] = 1;
}
Temp = temp-> fail;
}
}
}
Int main ()
{
Int n, m, I, j;
While (scanf ("% d", & n )! = EOF)
{
Root = new node;
Root-> fail = NULL;
Root-> init ();
Getchar ();
For (I = 1; I <= n; I ++)
{
Gets (str );
Id = I;
Insert ();
}
Getfail ();
Scanf ("% d", & m );
Getchar ();
Memset (ans, 0, sizeof (ans ));
For (I = 1; I <= m; I ++)
{
Memset (vis, 0, sizeof (vis ));
Id = I;
Gets (str );
Query ();
}
Int cnt = 0;
For (I = 1; I <= m; I ++)
{
If (ans [I] [0]> = 1)
{
Cnt ++;
Printf ("web % d:", I );
Int a [1111];
Memset (a, 0, sizeof ());
For (j = 0; ans [I] [j]> = 1; j ++)
{
A [ans [I] [j] = 1;
}
Int k;
For (k = 1; k <1100; k ++)
{
If (a [k])
Printf ("% d", k );
}
Printf ("\ n ");
}
}
Printf ("total: % d \ n", cnt );
}
Return 0;

}

# Include <stdio. h>
# Include <string. h>
# Include <malloc. h>
# Include <queue>
Using namespace std;
Int ans [1111] [5];
Char str [1111111];
# Define HTTP 130
Int id, vis [1111], a [1111];
Struct node
{
Int count;
Struct node * next [cc];
Struct node * fail;
Void init ()
{
Int I;
For (I = 0; I <cc; I ++)
Next [I] = NULL;
Count = 0;
Fail = NULL;
}
} * Root;
Void insert ()
{
Int len, k;
Node * p = root;
Len = strlen (str );
For (k = 0; k <len; k ++)
{
Int pos = str [k]-31;
If (p-> next [pos] = NULL)
{
P-> next [pos] = new node;
P-> next [pos]-> init ();
P = p-> next [pos];
}
Else p = p-> next [pos];
}
P-> count = id;
}

Void getfail ()
{
Int I;
Node * p, * temp, * son;
Queue <struct node *> que;
Que. push (root );
P = root;
While (! Que. empty ())
{
Temp = que. front ();
Que. pop ();
For (I = 0; I <cc; I ++)
{
Son = temp-> next [I];
If (son! = NULL)
{
If (temp = root)
{
Son-> fail = root;
}
Else
{
P = temp-> fail;
While (p)
{
If (p-> next [I]! = NULL)
{
Son-> fail = p-> next [I]; //
Break;
}
// Else note that there is no else
P = p-> fail;
}
If (p = NULL) son-> fail = root;
}
Que. push (son );
}
}
}
}

Void query ()
{
Int len, I, cnt = 0, pos;
Len = strlen (str );
Node * p, * temp;
P = root;
For (I = 0; I <len; I ++)
{
Pos = str [I]-31;
While (! P-> next [pos] & p! = Root) p = p-> fail;
P = p-> next [pos];
If (! P) p = root;
Temp = p;
While (temp! = Root) // you only need to modify it here.
{
If (temp-> count> = 1 &&! Vis [temp-> count])
{
Ans [id] [cnt ++] = temp-> count;
Vis [temp-> count] = 1;
}
Temp = temp-> fail;
}
}
}
Int main ()
{
Int n, m, I, j;
While (scanf ("% d", & n )! = EOF)
{
Root = new node;
Root-> fail = NULL;
Root-> init ();
Getchar ();
For (I = 1; I <= n; I ++)
{
Gets (str );
Id = I;
Insert ();
}
Getfail ();
Scanf ("% d", & m );
Getchar ();
Memset (ans, 0, sizeof (ans ));
For (I = 1; I <= m; I ++)
{
Memset (vis, 0, sizeof (vis ));
Id = I;
Gets (str );
Query ();
}
Int cnt = 0;
For (I = 1; I <= m; I ++)
{
If (ans [I] [0]> = 1)
{
Cnt ++;
Printf ("web % d:", I );
Int a [1111];
Memset (a, 0, sizeof ());
For (j = 0; ans [I] [j]> = 1; j ++)
{
A [ans [I] [j] = 1;
}
Int k;
For (k = 1; k <1100; k ++)
{
If (a [k])
Printf ("% d", k );
}
Printf ("\ n ");
}
}
Printf ("total: % d \ n", cnt );
}
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.