单词拆解&前缀树&树上DP LA 3942 Remember the Word
来源:互联网 发布:淘宝互刷红包群 编辑:程序博客网 时间:2024/05/16 06:06
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=505&page=show_problem&problem=4147
3942 - 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 , 1S4000 .
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
题意:给定一些单词,和一个长串,问这个长串拆分成已有单词,能拆分成几种方式
思路:
dp[i]=sum(dp[i+len(x)])
dp[i]表示从字符i开始的字符串即后缀(s[i..L])的分解方案数。
x为是(s[i..L]的前缀。
// LA3942 Remember the Word#include<cstring>#include<vector>using namespace std;const int maxnode = 4000 * 100 + 10;const int sigma_size = 26;// 字母表为全体小写字母的Triestruct 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] == '\0') 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\n", kase++, d[0]); } return 0;}
- 单词拆解&前缀树&树上DP LA 3942 Remember the Word
- LA 3942 Remember the Word(前缀树&树上DP)
- LA 3942 Remember the Word(字典树/树上dp)
- LA 3942 Remember the Word / 前缀树 + DP
- LA 3942 Remember the Word(Trie+DP)
- LA 3942 - Remember the Word 字典树 dp
- LA 3942 - Remember the Word 字典树+DP
- UVA 1401 & LA 3942 Remember the Word 字典树+DP
- (LA 3942)Remember the Word --DP+Trie树
- LA 3942 Remember the Word Trie树 DP
- LA 3942 Remember the Word——DP + 字典树
- uva1401 - Remember the Word 前缀树DP
- UVALive 3942 Remember the Word 字典树DP&&前缀树
- LA 3942 Remember the Word
- LA 3942 - Remember the Word
- LA 3942 - Remember the Word
- LA 3942 Remember the Word
- LA 3942 Remember the Word
- Listview_OnItemClickListener失效的问题
- 如何通过ContentHub把内容从一个地方传到另外一个地方
- 虚拟键盘与fixed带给移动端的痛!
- HDOJ 5392 Infoplane in Tina Town LCM
- 无法保存打印机设置 0x000006d9
- 单词拆解&前缀树&树上DP LA 3942 Remember the Word
- UILabel使用自定义字体
- C++读写.mat文件
- Android 升级数据库的最佳写法
- 蚂蚁金服张洁:基于深度学习的支付宝人脸识别技术解秘
- angular修改标签css修改的三种方法
- C++面试问题
- 自定义单元格
- 犀牛——第7章数组 7.9数组方法