• Uva1401(字典树)


    1401 - Remember the Word

    Time limit: 3.000 seconds

    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
    下面是《算法竞赛入门经典--训练指南》代码仓库里的标程
    // LA3942 Remember the Word
    // Rujia Liu
    #include<cstring>
    #include<vector>
    using namespace std;
    
    const int maxnode = 4000 * 100 + 10;
    const int sigma_size = 26;
    
    // 字母表为全体小写字母的Trie
    struct Trie {
      int ch[maxnode][sigma_size];
      int val[maxnode];
      int sz; // 结点总数
      void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } // 初始时只有一个根结点
      int idx(char c) { return c - 'a'; } // 字符c的编号
    
      // 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
      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]) { // 结点不存在
            memset(ch[sz], 0, sizeof(ch[sz]));
            val[sz] = 0;  // 中间结点的附加信息为0
            ch[u][c] = sz++; // 新建结点
          }
          u = ch[u][c]; // 往下走
        }
        val[u] = v; // 字符串的最后一个字符的附加信息为v
      }
    
      // 找字符串s的长度不超过len的前缀
      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]); // 找到一个前缀
        }
      }
    };
    
    #include<cstdio>
    const int maxl = 300000 + 10; // 文本串最大长度
    const int maxw = 4000 + 10;   // 单词最大个数
    const int maxwl = 100 + 10;   // 每个单词最大长度
    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
    ", kase++, d[0]);
      }
      return 0;
    }
    View Code
  • 相关阅读:
    iOS企业证书开发的APP证书过期时间监控
    事件冒泡,事件捕获
    倒计时
    获取多个div,点击第几个,显示第几个
    js继承
    javascript基础知识总结
    大型web系统高效应用方法(转载)
    数据库(内联,外联,交叉联)
    .net零碎基础知识点不完全小结
    C#的内存管理:堆、栈、托管堆与指针(转)
  • 原文地址:https://www.cnblogs.com/zywscq/p/3917820.html
Copyright © 2020-2023  润新知