Simple implementation of AC automatic machines

Source: Internet
Author: User

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 

 

 

 

 

Related Article

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.