uva 1401

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
Nanjing 2007-2008
3942 - Remember

 

dp[i] = sum(dp[i + len))

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 const int MAX = 3e5 + 7;
10 const int MOD = 20071027;
11 const int maxnode =  4000 * 100 + 8;
12 int ch[maxnode][26];
13 int val[maxnode];
14 int sz;
15 int idx(char c) { return c - 'a';}
16 
17 void insert(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;
24             ch[u][c] = sz++;
25         }
26         u = ch[u][c];
27     }
28 
29     val[u] = v;
30 }
31 
32 
33 char str[MAX];
34 int s;
35 int dp[MAX];
36 
37 void solve(int ca) {
38     memset(dp, 0, sizeof(dp));
39     int n = strlen(str);
40     dp[n] = 1;
41     for (int i = n - 1; i >= 0; --i) {
42         int u = 0;
43         for (int j = i; j < n; ++j) {
44             if (ch[u][ idx(str[j]) ]) {
45                 if (val[ ch[u][ idx(str[j]) ] ] == 1) dp[i] = (dp[i] + dp[j + 1]) % MOD;
46                 u = ch[u][ idx(str[j]) ];
47             } else {
48                 break;
49             }
50         }
51     }
52 
53     printf("Case %d: %d\n", ca, dp[0]);
54 }
55 
56 int main()
57 {
58    // freopen("sw.in", "r", stdin);
59     int ca = 1;
60     while (scanf("%s", str) != EOF) {
61         scanf("%d", &s);
62         sz = 1;
63         memset(ch[0], 0, sizeof(ch[0]));
64         char ss[105];
65         for (int i = 0; i < s; ++i) {
66             scanf("%s", ss);
67             insert(ss, 1);
68         }
69 
70         solve(ca++);
71 
72 
73     }
74     //cout << "Hello world!" << endl;
75     return 0;
76 }
View Code

 

posted @ 2014-08-13 15:08  hyx1  阅读(242)  评论(0编辑  收藏  举报