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>, 1S
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 = * + ;
- 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(字典树)的更多相关文章
- UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 剑指offer 面试65题
题目65题:不用加减乘除做加法. 解法一:Python特性 # -*- coding:utf-8 -*- class Solution: def Add(self, num1, num2): # wr ...
- 基于 普通及Lambda方式实现策略模式
什么是策略模式 策略模式代表了解决一类算法的通用解决方案,你可以在运行时选择使用哪种方案.比如如何使用不同的条件(比如苹果的重量,或者颜色 )来筛选库存中的苹果.你可以将这一模式应用到更广泛的领域 , ...
- Kattis - convexpolygonarea 【数学】
题意 给出一系列点,求这个多边形面积 思路 向量叉积 AC代码 #include <cstdio> #include <cstring> #include <ctype. ...
- iOS 11 Xcode9开发 新特性学习 (警告篇)
最新版本SDK优化了开发体验,编译过程会提供更多提示警告,建议你修改.这些功能也可以自主选择用或者不用,当然,苹果喜欢你用他推荐的东西... 1 . @avalibale 语法,同步判断当前iOS系统 ...
- $SublimeText2常用快捷键
1.删除一行:ctrl + shift + K2.替换:ctrl + H3.设置书签:Ctrl+F2设置书签F2 下一个书签Shift+F2上一个书签4.查找:ctrl + F 查找F3 查找下一个s ...
- 【leetcode刷题笔记】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Jquery 获取地址位置
直接在浏览器地址 输入: http://pv.sohu.com/cityjson?ie=utf-8 可以查看数据格式 引入一个搜狐的js库: <script src="http://p ...
- shell运行java/Jar 脚本
1.Shell执行/调用Java/Jar程序 #!/bin/bash JAVA_HOME="$HOME/jdk" BASE_DIR=`dirname $0` if [ " ...
- tomcat添加登录用户名密码
tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...
- 算法总结之 构造数组MaxTree
一个数组的MaxTree定义如下: 数组必须没有重复元素 MaxTree是一颗二叉树,数组的每一个值对应一个二叉树的节点 包括MaxTre树在内且在其中的每一颗子树上,值最大的节点都是树的头 给定一个 ...