UVA 1401 Remember the Word(用Trie加速动态规划)

Remember the Word

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not 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 sticks.

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set 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 words 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 <tex2html_verbatim_mark>, 1$ le$S$ le$4000 <tex2html_verbatim_mark>.

Each of the following S <tex2html_verbatim_mark>lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

There is a blank line between consecutive test cases.

You should proceed to the end of file.

Output 

For each test case, output the number, as described above, from the task description modulo 20071027.

Sample Input 

abcd 
4 
a 
b 
cd 
ab

Sample Output 

Case 1: 2

题目大意:背单词。给出一个由S个不同单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法?比如,有4个单词a,b,cd,ab,则abcd有两种分解方法:a+b+cd和ab+cd.

分析:不难想到这样的递推法:令d(i)表示从字符i开始的字符串(即后缀S[i..L])的分解方案数,则d(i)=sum{d(i+len(x))|单词x是S[i..L]的前缀}。
  如果先枚举x,再判断它是否为S[i..L]的前缀,时间无法承受(最多可能有4000个单词,判断还需要一定的时间)。可以换一个思路,先把所有单词组织成Trie,然后试着在Trie中“查找”S[i..L]。查找过程中每经过一个单词结点,就找到一个上述状态转移方程中的x,最多只需要比较100次就能能找到所有的x。

代码如下:
 1 #include<cstring>
 2 #include<vector>
 3 using namespace std;
 4 
 5 const int maxnode = 4000 * 100 + 10;
 6 const int sigma_size = 26;
 7 
 8 // 字母表为全体小写字母的Trie
 9 struct Trie {
10   int ch[maxnode][sigma_size];
11   int val[maxnode];
12   int sz; // 结点总数
13   void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } // 初始时只有一个根结点
14   int idx(char c) { return c - 'a'; } // 字符c的编号
15 
16   // 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
17   void insert(const char *s, int v) {
18     int u = 0, n = strlen(s);
19     for(int i = 0; i < n; i++) {
20       int c = idx(s[i]);
21       if(!ch[u][c]) { // 结点不存在
22         memset(ch[sz], 0, sizeof(ch[sz]));
23         val[sz] = 0;  // 中间结点的附加信息为0
24         ch[u][c] = sz++; // 新建结点
25       }
26       u = ch[u][c]; // 往下走
27     }
28     val[u] = v; // 字符串的最后一个字符的附加信息为v
29   }
30 
31   // 找字符串s的长度不超过len的前缀
32   void find_prefixes(const char *s, int len, vector<int>& ans) {
33     int u = 0;
34     for(int i = 0; i < len; i++) {
35       if(s[i] == '') break;
36       int c = idx(s[i]);
37       if(!ch[u][c]) break;
38       u = ch[u][c];
39       if(val[u] != 0) ans.push_back(val[u]); // 找到一个前缀
40     }
41   }
42 };
43 
44 #include<cstdio>
45 const int maxl = 300000 + 10; // 文本串最大长度
46 const int maxw = 4000 + 10;   // 单词最大个数
47 const int maxwl = 100 + 10;   // 每个单词最大长度
48 const int MOD = 20071027;
49 
50 int d[maxl], len[maxw], S;
51 char text[maxl], word[maxwl];
52 Trie trie;
53 
54 int main() {
55   int kase = 1;
56   while(scanf("%s%d", text, &S) == 2) {
57     trie.clear();
58     for(int i = 1; i <= S; i++) {
59       scanf("%s", word);
60       len[i] = strlen(word);
61       trie.insert(word, i);
62     }
63     memset(d, 0, sizeof(d));
64     int L = strlen(text);
65     d[L] = 1;
66     for(int i = L-1; i >= 0; i--) {
67       vector<int> p;
68       trie.find_prefixes(text+i, L-i, p);
69       for(int j = 0; j < p.size(); j++)
70         d[i] = (d[i] + d[i+len[p[j]]]) % MOD;
71     }
72     printf("Case %d: %d
", kase++, d[0]);
73   }
74   return 0;
75 }


 


原文地址:https://www.cnblogs.com/acm-bingzi/p/3201614.html