zoukankan      html  css  js  c++  java
  • UVA 1401 -- Remember the Word (字典树+dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4147

    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
    作者:_kangkang
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    一个滑动小块
    js 封装的函数 总结
    引用公用文件
    静态页面表单中js验证
    linux中Firefox浏览器 手动安装 flash
    将双击“root的主文件”弹出的窗口设置为文件浏览器
    eclipse中的aptana插件的安装
    System Center VMM请注意不同语言版本的差异
    CodeFirst模式开发涉及到mysql简单使用
    Echarts的使用
  • 原文地址:https://www.cnblogs.com/kangkang-/p/8410442.html
Copyright © 2011-2022 走看看