UVa 1401 (Tire tree) Remember the Word

Source: Internet
Author: User

D (i) means the number of decomposition methods of S[i, L-1] from the beginning of the suffix, the string is S[0, L-1]

Then there is d (i) = sum{D (I+len (x)) | Word x is the prefix of S[i, L-1]}

The recursive boundary is D (L) = 1, which represents an empty string.

Constructs a tire tree for the first n words, encounters a word node in the tree to find a suffix that represents the x in a state transition

1#include <cstdio>2#include <cstring>3 4 Const intMaxnode =400000+Ten;5 Const intMAXL = -+Ten;6 Const intSigma_size = -;7 Const intMOD =20071027;8 9 CharS[maxnode];Ten intl; One intD[maxnode]; A CharS1[MAXL]; -  - intsz; the intCh[maxnode][sigma_size]; - intVal[maxnode]; -  -InlineintIdxCharc) {returnC'a'; } +  - voidInit () {sz =1; memset (ch[0],0,sizeof(ch[0])); } +  A voidInsertChar* S,intv) at { -     intn = strlen (s), u =0; -      for(inti =0; I < n; i++) -     { -         intc =idx (s[i]); -         if(!Ch[u][c]) in         { -memset (Ch[sz],0,sizeof(Ch[sz])); toVAL[SZ] =0; +CH[U][C] = sz++; -         } theU =Ch[u][c]; *     } $Val[u] =v;Panax Notoginseng } -  the voidSolve () + { A      for(inti = l1; I >=0; i--) the     { +         intU =0; -          for(intj = i; J < L; J + +) $         { $             intc =idx (s[j]); -             if(!ch[u][c]) Break; -U =Ch[u][c]; the             if(Val[u]) d[i] = (D[i] + d[j +1]) %MOD; -         }Wuyi     } the } -  Wu intN; - intMain () About { $     //freopen ("In.txt", "R", stdin); -  -     intKase =0; -      while(SCANF ("%s", s) = =1) A     { + Init (); theL =strlen (s); -scanf"%d", &n); $          while(n--) {scanf ("%s", S1); Insert (S1,1); }//Val[i] = 1 indicates that this is a word node thememset (D,0,sizeof(d)); theD[L] =1; the solve (); theprintf"Case %d:%d\n", ++kase, d[0] %MOD); -     } in  the     return 0; the}
code June

UVa 1401 (Tire tree) Remember the Word

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.