Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=505&page=show_ problem&problem=4147
3942-remember the Word
Neal is very curious on combinatorial problems, and now here comes a problem about words. Knowing that Ray had a photographic memory and this could not be trouble him, Neal gives it to Jiejie.
Since Jiejie can ' t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of St Icks.
The problem is as follows:a word needs to being divided into small pieces in such a-a-to, each piece are from some given s Et of words. Given a word and the set of words, Jiejie should calculate the number of ways the Given Word can be divided, using the WOR DS in the set.
Input
The input file contains multiple test cases. For each test case:the first line contains the given word whose length is no more than 300 000.
The second line contains an integer s , 1s4000 .
Each of the following S lines contains one word from the set. Each word would be at the most characters long. There'll be no, identical words and all letters in the words would be lowercase.
There is a blank line between consecutive test cases.
You should proceed to the end of file.
Output
For each test case, the output of the number, as described above, from the task description modulo 20071027.
Sample Input
ABCD 4 a B cd AB
Sample Output
Case 1:2
Test instructions: Given some words, and a long string, ask this long string split into existing words, can split into several ways
Ideas:
Dp[i]=sum (Dp[i+len (x)])
Dp[i] Represents the string that begins with the character I, which is the suffix (s[i). L]) is the number of decomposition schemes.
X is yes (s[i). The prefix of L].
LA3942 Remember the word#include<cstring> #include <vector>using namespace std;const int maxnode = 4000 * 100 + 10;const int sigma_size = 26;//alphabet for all lowercase letters triestruct Trie {int ch[maxnode][sigma_size]; int Val[maxnode]; int sz; Total number of nodes void clear () {sz = 1; memset (Ch[0], 0, sizeof (ch[0]));}//initial only one root node int idx (char c) {return C-' a ';}//Word The number of characters c//insert string s, additional information is v. Note that V must not be 0 because 0 means "This node is not a word node" void Insert (const char *s, int v) {int u = 0, n = strlen (s); for (int i = 0; i < n; i++) {int c = idx (s[i]); if (!ch[u][c]) {//node does not exist memset (Ch[sz], 0, sizeof (Ch[sz])); Val[sz] = 0; The additional information for the intermediate junction is 0 ch[u][c] = sz++; New node} u = Ch[u][c]; Go down} val[u] = V; The additional information for the last character of the string is v}//Find the length of the string s not exceeding Len's prefix void find_prefixes (const char *s, int len, vector<int>& ans) { int u = 0; for (int i = 0; i < len; i++) {if (s[i] = = ' + ') break; int c = IDX (s[i]); if (!ch[u][c]) break; U = ch[u][c]; if (val[u]! = 0) Ans.push_back (Val[u]); Find a Prefix}}}; #include <cstdio>const int maxl = 300000 + 10; Text string maximum length const int MAXW = 4000 + 10; Maximum number of words const int MAXWL = 100 + 10; Max length per word const int MOD = 20071027;int D[maxl], Len[maxw], S;char TEXT[MAXL], WORD[MAXWL]; Trie Trie;int Main () {int kase = 1; while (scanf ("%s%d", text, &s) = = 2) {trie.clear (); for (int i = 1; I <= S; i++) {scanf ("%s", word); Len[i] = strlen (word); Trie.insert (Word, i); } memset (d, 0, sizeof (d)); int L = strlen (text); D[L] = 1; for (int i = L-1; I >= 0; i--) {vector<int> p; Trie.find_prefixes (Text+i, L-i, p); for (int j = 0; J < P.size (); j + +) D[i] = (D[i] + d[i+len[p[j]])% MOD; } printf ("Case%d:%d\n", kase++, d[0]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Word Disassembly & prefix tree & tree dp LA 3942 Remember the word