The AC automatic machine is a well-known multi-mode matching algorithm. It provides n words and an article containing m words to find out how many of them have appeared in the article.
The construction of an AC automatic machine consists of three steps: constructing a trie tree, constructing a fail pointer, and matching a pattern string. Common Single-mode string matching mainly uses the kmp algorithm. Multi-mode matching is similar to this algorithm. Therefore, to learn the AC automatic machine, you must have knowledge of the trie tree and kmp algorithm.
The following link provides a clear description of the AC automatic mechanism and its structure. The author first came into contact with the AC automatic mechanism during an interview.
Http://www.cppblog.com/mythit/archive/2009/04/21/80633.html
1 HDU 2222 2 3 # include <stdio. h> 4 5 struct node 6 {7 node * fail; 8 node * next [26]; 9 int flag; 10 node () 11 {12 fail = NULL; 13 for (int I = 0; I <26; I ++) 14 next [I] = NULL; 15 flag = 0; 16} 17 }; 18 19 char content [1000001]; 20 char seq [51]; 21 node * que [510000]; 22 23 void insert (char * str, node * root) 24 {25 node * p = root; 26 int t; 27 while (* str) 28 {29 t = * str-'A '; 30 if (p-> next [t] = NULL) 31 P-> next [t] = new node (); 32 p = p-> next [t]; 33 str ++; 34} 35 p-> flag ++; 36} 37 38 void build_ac (node * root) 39 {40 node * p, * q; 41 int head, tail; 42 head = tail = 0; 43 root-> fail = NULL; 44 que [head ++] = root; 45 while (tail