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>, 1S4000 <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 = * + ;
const int sigma_size = ; // 字母表为全体小写字母的Trie
struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz; // 结点总数
void clear() { sz = ; memset(ch[], , sizeof(ch[])); } // 初始时只有一个根结点
int idx(char c) { return c - 'a'; } // 字符c的编号 // 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
void insert(const char *s, int v) {
int u = , n = strlen(s);
for(int i = ; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) { // 结点不存在
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ; // 中间结点的附加信息为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 = ;
for(int i = ; i < len; i++) {
if(s[i] == '\0') break;
int c = idx(s[i]);
if(!ch[u][c]) break;
u = ch[u][c];
if(val[u] != ) ans.push_back(val[u]); // 找到一个前缀
}
}
}; #include<cstdio>
const int maxl = + ; // 文本串最大长度
const int maxw = + ; // 单词最大个数
const int maxwl = + ; // 每个单词最大长度
const int MOD = ; int d[maxl], len[maxw], S;
char text[maxl], word[maxwl];
Trie trie; int main() {
int kase = ;
while(scanf("%s%d", text, &S) == ) {
trie.clear();
for(int i = ; i <= S; i++) {
scanf("%s", word);
len[i] = strlen(word);
trie.insert(word, i);
}
memset(d, , sizeof(d));
int L = strlen(text);
d[L] = ;
for(int i = L-; i >= ; i--) {
vector<int> p;
trie.find_prefixes(text+i, L-i, p);
for(int j = ; j < p.size(); j++)
d[i] = (d[i] + d[i+len[p[j]]]) % MOD;
}
printf("Case %d: %d\n", kase++, d[]);
}
return ;
}

Uva1401(字典树)的更多相关文章

  1. UVA1401 Remember the Word 字典树维护dp

    题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...

  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  5. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  6. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  7. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  8. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  9. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. LeetCode:数据库技术【175-178】

    LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...

  2. 谷歌机器学习速成课程---3降低损失 (Reducing Loss):学习速率

    正如之前所述,梯度矢量具有方向和大小.梯度下降法算法用梯度乘以一个称为学习速率(有时也称为步长)的标量,以确定下一个点的位置.例如,如果梯度大小为 2.5,学习速率为 0.01,则梯度下降法算法会选择 ...

  3. Boostrap常用组件英文名

    dropdownlisttabsearchVertical TabSidebar with tabssidebarExpandable Panel ListFiltered Attendees Lis ...

  4. UI控件之UIView与动画

    UIView:用来展示用户的界面,响应用户的操作(继承自UIResponder) UIView的作用:绘图.动画.处理事件 UIView可以包含和管理子视图,并且决定子视图的位置大小 获取所有的子视图 ...

  5. OpenGL学习进程(7)第五课:点、边和图形(二)边

    本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概 ...

  6. java Excel导入导出工具类

    本文章,导入导出依赖提前定义好的模板 package com.shareworx.yjwy.utils; import java.io.File; import java.io.FileInputSt ...

  7. Python编程-异常处理

    一.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def t ...

  8. [POI2009]Wie

    题目 BZOJ 虽然是解压题但也学到了简洁的码风 做法 \(dijkstra\)跑动规 My complete code #include<bits/stdc++.h> #include& ...

  9. No input clusters found in output/ZS_TEST_OUTPUT3404268420/clusters-0/part-randomSeed. Check your -c argument.

    错误原因可能为: -i 后面参数路径对应的目录或文件中的数据为空,即输入集为空,所以找不到cluster

  10. centos中如何寻找Nginx,Apache,PHP,mysql的配置路径

    很多小伙伴都可能会碰到安装好环境之后忘记了或者不知道怎么查看配置环境的文件路径了, 下面我就来介绍centos中nginx.apache.php.mysql配置文件路径查看方法吧. 1.判断apach ...