hihocoder#1036 trie Map

Source: Internet
Author: User

Original title Address

After reading this blog post, I finally figured out the trie figure.

Runtime error has not been several times, has not known why, so wrote a script to generate a set of big data, found that it was wrong.

Commissioning of the afternoon, finally make it clear, why?

1. The variable space is not placed in the function, will explode the stack, should be made into a global variable or is dynamically allocated from the heap.

2. See the data range of the topic, at first my max_node was set to 1024 ...

Code:

1#include <iostream>2#include <cstring>3 4 using namespacestd;5 6 #defineMax_node 10000107 #defineSigma_size 328 9 intQ[max_node];Ten  One structTriegraph { A   intF[max_node]; -   intG[max_node][sigma_size]; -   intM[max_node]; the   intsize; -  -   voidinit () { -Size =1; +Memset (F,0,sizeof(f)); -memset (g[0],0,sizeof(g[0])); +   } A  at   intIndexCharc) { -     returnC'a'; -   } -  -   voidInsertConst Char*s) { -     intU =0; in      while(*s) { -       inti = index (*s); to       if(!G[u][i]) { +memset (G[size],0,sizeof(G[size])); -M[size] =0; theG[u][i] = size++; *       } $U =G[u][i];Panax Notoginsengs++; -     } theM[u] =1; +   } A  the   voidbuild () { +     intQH =0, QT =0; -f[0] =0; $      for(inti =0; I < -; i++) { $       int&p = g[0][i]; -       if(p) { -F[P] =0; theq[qt++] =p; -       }Wuyi       Else thep =0; -     } Wu      while(QH <qt) { -       intU = q[qh++]; About        for(inti =0; I < -; i++) { $         int&v =G[u][i]; -         if(v) { -q[qt++] =v; -F[V] =G[f[u]][i]; AM[u] |=M[f[u]]; +         } the         Else -v =G[f[u]][i]; $       } the     } the   } the  the   BOOLFindConst Char*s) { -     intU =0; in      while(*s) { the       inti = index (*s); the        while(U &&!)G[u][i]) AboutU =F[u]; theU =G[u][i]; the       if(M[u]) the         return true; +s++; -     } the     return false;Bayi   } the } TG; the  - intMain () { -   intN; the   strings; the  the tg.init (); theCIN >>N; -    for(inti =0; i < N; i++) { theCIN >>s; the Tg.insert (S.c_str ()); the   }94 tg.build (); theCIN >>s; thecout << Tg.find (S.c_str ())?"YES":"NO") <<Endl; the   return 0;98}

hihocoder#1036 trie Map

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.