HDU 2222 keywords search (my first AC automatic machine, template question)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2222

Analysis and Summary:

As a well-known entry-level question of AC automatic machine, I have already said nothing about it. Used to test what I wrote after I learned the AC automatic machine.Code.

This template also has two forms. One is static array, and the other is in Liu lujia's big white book, another type is the pointer format I used (but it is also the first to open up the array node statically, and then allocate the pointer to the array space, the speed and the size of the opened memory is not bad ), after a long tangle, I found that I still like the pointer format, So I insisted on using it.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; const int max_node = 1000005; const int sigma_size = 26; int size; char STR [max_node]; char keyword [55]; struct node {node * fail; node * Next [sigma_size]; int count; void Init () {fail = NULL; count = 0; memset (next, 0, sizeof (next) ;}}; class ac_automation {public: void Init (); void insert (char * Str ); void getfail (); int fin D (char * Str); Private: node * new_node (); node heap [max_node]; node * root; int size ;}; node * ac_automation: new_node () {heap [size]. init (); Return & heap [size ++];} void ac_automation: Init () {size = 0; root = new_node ();} void ac_automation :: insert (char * Str) {node * P = root; For (; * STR; ++ Str) {int CH = * str-'A '; if (p-> next [CH] = NULL) P-> next [CH] = new_node (); P = p-> next [CH];} + P-> count;} void ac_automation: G Etfail () {queue <node *> q; q. Push (Root); While (! Q. empty () {node * TMP = Q. front (); q. pop (); node * P; For (INT I = 0; I <sigma_size; ++ I) {node * & now = TMP-> next [I]; if (now! = NULL) {q. Push (now); If (TMP = root) {now-> fail = root; continue;} p = TMP-> fail; while (P! = NULL) {If (p-> next [I]! = NULL) {now-> fail = p-> next [I]; break;} p = p-> fail;} If (P = NULL) now-> fail = root;} else {// construct a trie graph to Improve the efficiency if (TMP = root) Now = root; else now = TMP-> fail-> next [I] ;}}} int ac_automation: Find (char * Str) {node * P = root; int CNT = 0; for (; * STR; ++ Str) {char CH = * str-'A'; P = p-> next [CH]; If (P = NULL) P = root; node * TMP = P; while (TMP! = Root & TMP-> count! =-1) {CNT + = TMP-> count; TMP-> COUNT =-1; TMP = TMP-> fail;} return CNT;} ac_automation AC; int main () {int ncase, M; scanf ("% d", & ncase); While (ncase --) {scanf ("% d % * C ", & M); // init AC automation AC. init (); While (M --) {gets (keyword); AC. insert (keyword);} gets (STR); AC. getfail (); printf ("% d \ n", AC. find (STR);} return 0 ;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,By
D_double (reprinted please mark)

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.