码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 3942 -- Remember the Word (字典树+dp)

时间:2018-02-03 20:02:30      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:self   multiple   using   tween   memory   amount   ali   this   bcd   

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, 1 ≤ S ≤ 4000.

Each of the following S 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个不同的单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可重复使用),有多少种方法?

解析:状态转移方程:dp[j] = sum{ dp[j-len(s)] }, dp[j]表示长字符串前j个字母的分解种数,s为字典中的单词且为长字符串前j个字母的后缀,i-len(s)为已经确定分解种数的长度;  暴力枚举显然超时,故用字典树优化查找。

 

技术分享图片
 1 #include <iostream>
 2 #include <cstring>
 3 #define MAXN 300010
 4 #define MAXNODE 400010
 5 #define MOD 20071027
 6 using namespace std;
 7 int N, sum[MAXN];
 8 char str[MAXN];
 9 int ch[MAXNODE][26];
10 struct Trie{
11     bool flag[MAXNODE];
12     int sz;
13     Trie() {
14         memset(ch, 0, sizeof(ch));
15         memset(flag, false, sizeof(flag));
16         sz = 1;
17     }
18     void Insert(char * s) {
19         int u = 0;
20         for (int i = 0; s[i]; i++) {
21             if (!ch[u][s[i]-a]) ch[u][s[i]-a] = sz++;
22             u = ch[u][s[i]-a];
23         }
24         flag[u] = true;
25     }
26     void query(char * s) {
27         memset(sum, 0, sizeof(sum)); sum[0] = 1;  // 注意将sum[0]初始化为1
28         for (int i = 1; s[i]; i++) {  // 已经确定分解种数的长度为i-1
29             int u = 0;
30             for (int j = i; s[j]; j++) {  // 字典中的单词最长为100,此处最多循环100次
31                 if (!ch[u][s[j]-a]) break;
32                 u = ch[u][s[j]-a];
33                 if (flag[u]) sum[j] = (sum[j] + sum[i-1]) % MOD;
34             }
35         }
36     }
37 };
38 
39 int main() {
40     ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
41     int cnt = 0;
42     while (cin >> str + 1) {
43         cin >> N;
44         Trie trie;
45         char s[110];
46         for (int i = 0; i < N; i++) {
47             cin >> s;
48             trie.Insert(s);
49         }
50         trie.query(str);
51         cout << "Case " << ++cnt << ": " << sum[strlen(str+1)] << endl;
52     }
53     return 0;
54 }
View Code

 

UVA 3942 -- Remember the Word (字典树+dp)

标签:self   multiple   using   tween   memory   amount   ali   this   bcd   

原文地址:https://www.cnblogs.com/kangkang-/p/8410442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!