AC automatic machine (Aho-corasick automation)

Source: Internet
Author: User

AC automatic machine: Aho-corasick automation, which was developed by Bell Laboratory in 1975 and is a famous multi-mode matching algorithm.

Solve the problem: give n words, and then give an article containing M characters, so that you can find out how many words have appeared in the article. (It is also a common Algorithm for Solving keyword links in Web development: N keywords, one article, to find out where keywords appear in the article)

Idea: Make KMP on a trie tree. Each node has a pointer (failure pointer) that matches failures like KMP ), if the matching fails, the matching continues Based on the node pointed to by the failure pointer.

To understand the AC automatic mechanism, you must first have the basic knowledge of the pattern tree (Dictionary tree) trie and KMP pattern matching algorithms. The AC automatic machine algorithm consists of three steps: constructing a trie tree and constructing the failure pointer and pattern matching process. In KMP, we use two pointers, I and j, respectively. A [I-j + 1. I] is exactly the same as B [1. J. That is to say, I is constantly increasing. As I increases, J changes accordingly, and a string whose length ends with a [I] J exactly matches the first J characters of string B. When a [I + 1] is less than B [J + 1], the KMP policy is to adjust the J position (reduce the J value) so that a [I-j + 1 .. i] and B [1 .. j] The new B [J + 1] exactly matches a [I + 1], and the next function records the position where J should be adjusted. The failure pointer of the same AC automatic mechanism has the same function. That is to say, when our mode string matches on tire, if it cannot match the keyword of the current node, you should continue matching with the node pointed to by the failure pointer of the current node.

Example: Give five words: Say she SHR he her, and then give a string yasherhs. Ask how many words have appeared in this string. Specify the data structure required by the AC automatic machine.

1 const int kind = 26;
2 struct node {
3 node * fail; // failure pointer
4 node * Next [kind]; // tire subnode of each node (up to letters)
5 Int count; // whether it is the last node of the word
6 node () {// constructor Initialization
7 fail = NULL;
8 count = 0;
9 memset (next, null, sizeof (next ));
10}
11} * Q [500001]; // queue, facilitating the BFS Construction Failure pointer
12 char keyword [51]; // input word
13 char STR [1000001]; // mode string
14 int head, tail; // head and tail pointer of the queue

First, construct the five words into a tire ,.

After constructing the tire, the next step is to construct the failure pointer. The process of constructing the failure pointer is summarized as one sentence: Set the letter on this node to C and follow his father's failure pointer until he reaches a node, his son also has a node with the letter C. Then point the failure pointer of the current node to the son of C. If the root fails to be found, point the failure pointer to the root. For specific operations, you only need to: First add the root to the queue (the root failure Pointer Points to itself or null). Then, every time we process a vertex, we add all its sons to the queue, the queue is empty.

1 void build_ac_automation (node * root ){
2 int I;
3 root-> fail = NULL;
4 Q [head ++] = root;
5 while (Head! = Tail ){
6 node * temp = Q [tail ++];
7 node * P = NULL;
8 For (I = 0; I <26; I ++ ){
9 If (temp-> next [I]! = NULL ){
10 if (temp = root) temp-> next [I]-> fail = root;
11 else {
12 p = temp-> fail;
13 while (P! = NULL ){
14 if (p-> next [I]! = NULL ){
15 temp-> next [I]-> fail = p-> next [I];
16 break;
17}
18 P = p-> fail;
19}
20 if (P = NULL) temp-> next [I]-> fail = root;
21}
22 Q [head ++] = temp-> next [I];
23}
24}
25}
26}

Observe the process of constructing a failure pointer from the code: see figure-2. First, the root fail Pointer Points to null, and then the root enters the queue and enters the loop. During the 1st cycle, we need to process two nodes: Root-> next ['H'-'a'] (node H) and root-> next ['s '-'a'] (node S ). Point the failure pointer of the two nodes to the root node and successively enter the queue. The failure Pointer Points to the two dotted lines (1) and (2) in Figure 2; after entering the cycle for 2nd times, the queue first pops up h, and then P points to the fail pointer of the H node to the node, that is, root; after entering the cycle of Row 3, P = p-> fail, that is, P = NULL, then exit the loop and point the Fail pointer of node e to the root, corresponding to (3) in figure-2 ), node E then enters the queue. When the first cycle occurs, the operation on node A displayed is the same as that on node e in the previous step. The fail pointer of node A is directed to the root, corresponds to (4) in figure 2 and enters the queue. When you enter the cycle for 4th times, node H (the one on the left in the figure) is displayed, and the operations are slightly different. When the program runs until 14 rows, Because p-> next [I]! = NULL (root has the son node H, the one on the right in the figure). In this way, point the failure pointer of the H node on the left to the son node H on the right, corresponds to (5) in Figure 2, and then enters the queue with H. And so on: After the loop ends, all the failure pointers are in the form shown in the figure.

Finally, we can find the words in the mode string on the AC automatic machine. The matching process is divided into two situations: (1) the current character matches, indicating that there is a path along the tree edge from the current node to reach the target character. In this case, you only need to continue matching along the path to the next node, the target string pointer moves to the next character to continue matching. (2) If the current character does not match, the character pointing to the failed pointer of the current node continues matching. The matching process ends with the pointer pointing to the root. Repeat any of the two processes until the pattern string ends.

1 int query (node * root ){
2 int I = 0, CNT = 0, index, Len = strlen (STR );
3 node * P = root;
4 While (STR [I]) {
5 Index = STR [I]-'A ';
6 While (p-> next [Index] = NULL & P! = Root) P = p-> fail;
7 p = p-> next [Index];
8 p = (P = NULL )? Root: P;
9 node * temp = P;
10 while (temp! = Root & temp-> count! =-1 ){
11 CNT + = temp-> count;
12 temp-> COUNT =-1;
13 temp = temp-> fail;
14}
15 I ++;
16}
17 return CNT;
18}

Take a look at the detailed process of pattern matching. The pattern string is yasherhs. For I = 0, 1. There is no corresponding path in trie, so no operation is performed. When I = 2, 3, 4, the pointer P goes to the lower left node E. Because the Count information of node e is 1, CNT + 1, and the count value of node e is set to-1, which indicates that the word has already been changed to prevent repeated counting, finally, temp points to the failed pointer of node e to continue searching, and so on. Finally, temp points to root and exits the while loop. In this process, count is increased by 2. 2 words she and he are found. When I = 5, the program enters row 5th, and P points to the node with its failure pointer, that is, the e node on the right, and then points to the r node in row 6th, the Count value of the r node is 1, so that count + 1 is collected until temp points to root. When I = 6, 7, no matching is found, and the matching process ends.

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.