【DP总结】【字符串】【前缀树】【LA3942】
2016-11-07 20:44
197 查看
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 , 1
S
4000 .
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]的前缀。
[cpp] view
plain copy
print?
// LA3942 Remember the Word
#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] == '\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总结】【字符串】最短编辑距离
- 【DP总结】【字符串】【POJ1141】
- 【DP总结】【字符串】【统计单词个数】
- 【DP总结】【字符串】【子串】
- JS字符串 window.open() window.opener window.name window对象总结
- [转]有关UNICODE、ANSI字符集和相关字符串操作的总结!
- "ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小"的解决,总结一下oracle开发的小经验
- C#字符串操纵总结
- javascript的字符串处理函数总结
- COM中的几种字符串类型的总结
- SQL server 2000常用字符串长度总结
- 字符串数组的一些处理方法总结
- Java字符串使用总结
- 有关UNICODE、ANSI字符集和相关字符串操作的总结!
- 有关UNICODE、ANSI字符集和相关字符串操作的总结
- DataFormatString格式化字符串的总结
- 【转载】关于C#格式化字符串,最好的总结
- 有关UNICODE、ANSI字符集和相关字符串操作的总结
- C++字符串完全指南(2) - 总结
- [转贴]字符串数组的一些处理方法总结