Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & amp; Itemid = 8 & page = show_problem & amp; Category = & amp; problem = 4147
Question: Given a string and several words, there are several ways to use words to form a string. First, I had a problem with the DP equation. I don't know how to modify it for a long time, then, the array is too small to be replayed.
Trie array size = Number of words * Word Length
DP [I] is the extension of ANS starting with str [I], DP [I] = segma (DP [k]), where k Represents STR [I... k-1] is a word, if k = Len, then DP [I] ++;
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; # define ll long longconst int n = 4000*100 + 100; const int mod = 20071027; char STR [300019], Pat [115]; ll DP [300019]; const int TK = 26, TB = 'a'; int top, tree [N] [TK + 1], Len; void Init () {Top = 1; memset (tree [0], 0, sizeof (tree [0]);} int sear (char * s, int I) {int CNT = 0; ll ans = 0; For (INT RT = 0; RT = tree [RT] [* s-TB]; ++ s) {If (* (S) = 0) break; CNT ++; If (Tree [RT] [TK]) // CNT! = Tree [RT] [TK] indicates DP [I .. len-1] is a word, at this time there is no increase in the number of splits {If (* (S + 1) = 0) ans ++; ans = (ANS + dp [I + CNT]) % MOD; ///// // printf ("RT = % d S = % s I = % d CNT = % d dp = % LLD \ n ", RT, STR + I + CNT, I, CNT, DP [I]); /// //} return ans;} void insert (char * s, int Rank = 0) // rank is the length of {int RT, NXT; For (RT = 0; * s; RT = NXT, ++ S, rank ++) {NXT = tree [RT] [* s-TB]; If (0 = NXT) // NXT! When it is set to 0, there is a public prefix. It has already been done before. You just need to continue the jump and insert "her" to "h" and "E" to "NXT! = 0 no need to insert {tree [RT] [* s-TB] = NXT = top; memset (tree [Top], 0, sizeof (tree [Top]); top ++ }}tree [RT] [TK] = rank;} int main () {// freopen ("uva1401.txt", "r", stdin); int N, ncase = 1, Pos; while (scanf ("% s", STR )! = EOF) {Init (); Pos = 0; Len = strlen (STR); scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% s", Pat); insert (PAT) ;}dp [Len] = 0; For (INT I = len-1; i> = 0; I --) {DP [I] = 0; DP [I] = sear (STR + I, I);} printf ("case % d: % LLD \ n ", ncase ++, DP [0]);} return 0 ;}