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