Hdu 2896 virus attack-AC automatic machine

Source: Internet
Author: User

[Cpp]
/*
Search for all substrings
It cannot be a letter or number, so there are about 130 subnodes
*/
# Include
# Include
# Include
Using namespace std;

Int biaoshi [510];
Const int kind = 130;
Int dulist [10];
Struct node {
Node * fail; // failure pointer
Node * next [kind];
Int count; // whether it is the last node of the word
Node () {// constructor Initialization
Fail = NULL;
Count = 0;
Memset (next, NULL, sizeof (next ));
}
} * Q [5000000]; // queue, which is used to construct a failure pointer for bfs
Char keyword [510]; // input word
Char str [1000010]; // mode string
Int head, tail; // The head and tail pointer of the queue.

Void insert (char * str, node * root, int no ){
Node * p = root;
Int I = 0, index;
While (str [I]) {
Index = str [I]-31;
If (p-> next [index] = NULL) p-> next [index] = new node ();
P = p-> next [index];
I ++;
}
P-> count = no; // Initialization is 0, + + is followed by 1, indicating the end of a word
}
Void build_ac_automation (node * root)
{
Int I;
Root-> fail = NULL;
Q [head ++] = root;
While (head! = Tail)
{
Node * temp = q [tail ++]; // get a node
Node * p = NULL;
For (I = 0; I <130; I ++)
{
If (temp-> next [I]! = NULL) // if its child I is not empty
{
If (temp = root) // the child's failure Pointer Points to the header.
Temp-> next [I]-> fail = root;
Else {// common Node
P = temp-> fail; // pointer to your failure
While (p! = NULL)
{
If (p-> next [I]! = NULL) // The failure pointer has an I child
{
Temp-> next [I]-> fail = p-> next [I]; // The failed pointer of the child I of the current node points to the child I of the failed pointer, and then jumps out
Break;
}
P = p-> fail; // continue to find the failed pointer
}
If (p = NULL) // if the failure pointer is NULL
Temp-> next [I]-> fail = root; // The failed pointer of the child I of the current node points to the header
}
Q [head ++] = temp-> next [I]; // all input objects have defined failure pointers. Therefore, the process is to define failure pointers for their children.
}
}
}
}
Int query (node * root)
{
Int I = 0, cnt = 0, index, len = strlen (str );
Node * p = root;
While (str [I])
{
Index = str [I]-31; // calculates the child's location
While (p-> next [index] = NULL & p! = Root)
P = p-> fail; // if no I child node exists, use the failure pointer to find the same node with I child as before.
P = p-> next [index]; // point to its child
P = (p = NULL )? Root: p;
Node * temp = p;
While (temp! = Root & temp-> count)
{
If (temp-> count &&! Biaoshi [temp-> count]) // you can change the value here.
{
Cnt ++;
Biaoshi [temp-> count] = 1;
}
Temp = temp-> fail;
}
I ++;
}
Return cnt;
}
Int cmp (const void * a, const void * B)
{
Int * c = (int *) a, * d = (int *) B;
Return * c-* d;
}
Void print (int ji, int na, int n)
{
 
Printf ("web % d:", na );
For (int I = 1; I <= n; ++ I)
{
If (biaoshi [I])
{
Printf ("% d", I );
}
}
Printf ("\ n ");
}
Int main (){
Int n, t, I, j = 0;
While (scanf ("% d", & n )! = EOF)
{
Head = tail = 0;
Node * root = new node ();

Getchar ();
For (I = 1; I <= n; I ++)
{
Gets (keyword );
Insert (keyword, root, I );
}
Build_ac_automation (root );
Scanf ("% d", & t );
For (I = 1; I <= t; ++ I)
{
Memset (biaoshi, 0, sizeof (biaoshi ));
Scanf ("% s", str );
Int ge = query (root );
If (ge)
{
Print (ge, I, n );
J ++;
}
}
Printf ("total: % d \ n", j );
}
Return 0;
}

/*
Search for all substrings
It cannot be a letter or number, so there are about 130 subnodes
*/
# Include
# Include
# Include
Using namespace std;
 
Int biaoshi [510];
Const int kind = 130;
Int dulist [10];
Struct node {
Node * fail; // failure pointer
Node * next [kind];
Int count; // whether it is the last node of the word
Node () {// constructor Initialization
Fail = NULL;
Count = 0;
Memset (next, NULL, sizeof (next ));
}
} * Q [5000000]; // queue, which is used to construct a failure pointer for bfs
Char keyword [510]; // input word
Char str [1000010]; // mode string
Int head, tail; // The head and tail pointer of the queue.
 
Void insert (char * str, node * root, int no ){
Node * p = root;
Int I = 0, index;
While (str [I]) {
Index = str [I]-31;
If (p-> next [index] = NULL) p-> next [index] = new node ();
P = p-> next [index];
I ++;
}
P-> count = no; // Initialization is 0, + + is followed by 1, indicating the end of a word
}
Void build_ac_automation (node * root)
{
Int I;
Root-> fail = NULL;
Q [head ++] = root;
While (head! = Tail)
{
Node * temp = q [tail ++]; // get a node
Node * p = NULL;
For (I = 0; I <130; I ++)
{
If (temp-> next [I]! = NULL) // if its child I is not empty
{
If (temp = root) // the child's failure Pointer Points to the header.
Temp-> next [I]-> fail = root;
Else {// common Node
P = temp-> fail; // pointer to your failure
While (p! = NULL)
{
If (p-> next [I]! = NULL) // The failure pointer has an I child
{
Temp-> next [I]-> fail = p-> next [I]; // The failed pointer of the child I of the current node points to the child I of the failed pointer, and then jumps out
Break;
}
P = p-> fail; // continue to find the failed pointer
}
If (p = NULL) // if the failure pointer is NULL
Temp-> next [I]-> fail = root; // The failed pointer of the child I of the current node points to the header
}
Q [head ++] = temp-> next [I]; // all input objects have defined failure pointers. Therefore, the process is to define failure pointers for their children.
}
}
}
}
Int query (node * root)
{
Int I = 0, cnt = 0, index, len = strlen (str );
Node * p = root;
While (str [I])
{
Index = str [I]-31; // calculates the child's location
While (p-> next [index] = NULL & p! = Root)
P = p-> fail; // if no I child node exists, use the failure pointer to find the same node with I child as before.
P = p-> next [index]; // point to its child
P = (p = NULL )? Root: p;
Node * temp = p;
While (temp! = Root & temp-> count)
{
If (temp-> count &&! Biaoshi [temp-> count]) // you can change the value here.
{
Cnt ++;
Biaoshi [temp-> count] = 1;
}
Temp = temp-> fail;
}
I ++;
}
Return cnt;
}
Int cmp (const void * a, const void * B)
{
Int * c = (int *) a, * d = (int *) B;
Return * c-* d;
}
Void print (int ji, int na, int n)
{

Printf ("web % d:", na );
For (int I = 1; I <= n; ++ I)
{
If (biaoshi [I])
{
Printf ("% d", I );
}
}
Printf ("\ n ");
}
Int main (){
Int n, t, I, j = 0;
While (scanf ("% d", & n )! = EOF)
{
Head = tail = 0;
Node * root = new node ();

Getchar ();
For (I = 1; I <= n; I ++)
{
Gets (keyword );
Insert (keyword, root, I );
}
Build_ac_automation (root );
Scanf ("% d", & t );
For (I = 1; I <= t; ++ I)
{
Memset (biaoshi, 0, sizeof (biaoshi ));
Scanf ("% s", str );
Int ge = query (root );
If (ge)
{
Print (ge, I, n );
J ++;
}
}
Printf ("total: % d \ n", j );
}
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.