UVA 1401 Remember the Word(用Trie加速动态规划)
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 <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 题目大意:背单词。给出一个由S个不同单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法?比如,有4个单词a,b,cd,ab,则abcd有两种分解方法:a+b+cd和ab+cd. 分析:不难想到这样的递推法:令d(i)表示从字符i开始的字符串(即后缀S[i..L])的分解方案数,则d(i)=sum{d(i+len(x))|单词x是S[i..L]的前缀}。
如果先枚举x,再判断它是否为S[i..L]的前缀,时间无法承受(最多可能有4000个单词,判断还需要一定的时间)。可以换一个思路,先把所有单词组织成Trie,然后试着在Trie中“查找”S[i..L]。查找过程中每经过一个单词结点,就找到一个上述状态转移方程中的x,最多只需要比较100次就能能找到所有的x。 代码如下:
#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 ;
}
UVA 1401 Remember the Word(用Trie加速动态规划)的更多相关文章
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...
- UVA 1401 Remember the Word
字典树优化DP Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- uva 1401 dp+Trie
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1401 (Tire树) Remember the Word
d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...
随机推荐
- 《Android View 的事件分发和滑动冲突》 —预习资料
1. 阅读书籍<Android开发艺术探索>第三章 2. 提前阅读如下技术文章: http://blog.csdn.net/singwhatiwanna/article/details/3 ...
- HDOJ-ACM1022(JAVA)
这道题:是模拟出栈,判断出栈顺序的可能性. 基本上大家的做法都是直接模拟栈的出栈入栈并将顺序用0,1序列来表示,我暂时没想到什么好的思路. import java.util.*; import jav ...
- puppet重申证书
直接上步骤,由于测试用的是PE3.X版本,在网上搜的命令几乎与PE相关的puppet命令不同了, 1.在PE-Client操作,停止pe-puppet,pe-mcollective资源; puppet ...
- 算法之旅,直奔<algorithm>之十三 fill
fill(vs2010) 引言 这是我学习总结<algorithm>的第十三篇,fill是一个很好的初始化工具.大学挺好,好好珍惜... 作用 fill 的作用是 给容器里一个指定的范围 ...
- 算法之旅,直奔<algorithm>之十四 fill_n
fill_n(vs2010) 引言 这是我学习总结<algorithm>的第十四篇,作为fill的亲兄弟,fill_n也会助你一把的. 作用 fill_n 的作用是给一段指定长度的数据向量 ...
- IOS7 自定义UIBarButtonItem 的一些问题
ios 下自定义导航栏的BarButtonItem 会产生一些偏移问题, 解决方案: 通过新建一个系统的带固定距离的Item来调节你的Item #define IOS7_NAVI_SPACE ...
- 数字信号处理与音频处理(使用Audition)
前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...
- 【转】cocos2d-x 开发中使用的一些工具
http://k57box.blog.163.com/blog/static/142261374201327112050361/ 这些工具平常也用到,不过没有像这样整理出来,这是我在网上看到的.就记录 ...
- [转]JavaScript作用域安全构造函数
构造函数其实就是一个使用new操作符调用的函数.当使用new调用时,构造函数内用到的this对象会对指向新创建的对象实例,如下的例子所示: function Person(name, ag ...
- OC最实用的runtime总结,面试、工作你看我就足够了!
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的,并且最后两个demo也是MJExtension ...