Trie + DP LA 3942 Remember the Word
题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个
分析:书上的做法。递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+len(s)])。用字典树记录并查询短单词的前缀的长度。
#include <bits/stdc++.h>
using namespace std; const int L = 3e5 + 5;
const int N = 4e3 + 5;
const int M = 1e2 + 5;
const int MOD = 20071027;
char text[L], word[M];
struct Trie {
int ch[N*M][26], val[N*M], sz;
int idx(char c) {
return c - 'a';
}
void clear(void) {
sz = 1; memset (ch[0], 0, sizeof (ch[0]));
}
void insert(char *str, int v) {
int u = 0, len = strlen (str);
for (int c, i=0; i<len; ++i) {
c = idx (str[i]);
if (!ch[u][c]) {
memset (ch[sz], 0, sizeof (ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
}
void query(char *str, int len, vector<int> &vec) {
int u = 0;
for (int c, i=0; i<len; ++i) {
c = idx (str[i]);
if (!ch[u][c]) return ;
u = ch[u][c];
if (val[u]) vec.push_back (val[u]);
}
}
}trie;
int dp[L], l[N]; int main(void) {
int cas = 0, n;
while (scanf ("%s", &text) == 1) {
scanf ("%d", &n);
trie.clear ();
for (int i=1; i<=n; ++i) {
scanf ("%s", &word);
l[i] = strlen (word);
trie.insert (word, i);
}
memset (dp, 0, sizeof (dp));
int len = strlen (text); dp[len] = 1;
for (int i=len-1; i>=0; --i) {
vector<int> vec;
trie.query (text+i, len-i, vec);
for (int j=0; j<vec.size (); ++j) {
dp[i] = (dp[i] + dp[i+l[vec[j]]]) % MOD;
}
}
printf ("Case %d: %d\n", ++cas, dp[0]);
} return 0;
}
Trie + DP LA 3942 Remember the Word的更多相关文章
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- [LA 3942] Remember the Word
Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- LA 3942 - Remember the Word 字典树+DP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
随机推荐
- eclipse clear swtich workspace
edit : --> D:\tools\eclipse\configuration\.settings\org.eclipse.ui.ide.prefs
- [转]DB2类型转换函数
Src URL:http://www.cnblogs.com/QQParadise/articles/2642677.html
- codevs 2530大质数
链接:http://codevs.cn/problem/1530/ 解题思路: 这个题最关键的剪枝还是 因子小于平方根,但不是像原来那样用. 逆转思维,与其说判断哪些是质数,不如说判断哪些不是质数,更 ...
- CLR via C#(07)-静态类,分部类
一. 静态类-Static 静态类是一些不能实例化的类,它的作用是将一些相关的成员组合到一起,像我们常见的Math, Console等.静态类由static关键字标识,静态类成员也只能是st ...
- 与你相遇好幸运,Waterline初遇
Github : https://github.com/balderdashy/waterline 文档 : https://github.com/balderdashy/waterline-docs ...
- Delphi字符串的基本操作与常用函数
参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...
- Delphi的文件操作
参考自:http://www.cnblogs.com/railgunman/articles/1800318.html Delphi 中默认有input 和 output 两个文件变量,使用可以不用定 ...
- [LeetCode] Remove Element (三种解法)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- POJ3691 DNA repair(AC自动机 DP)
给定N个长度不超过20的模式串,再给定一个长度为M的目标串S,求在目标串S上最少改变多少字符,可以使得它不包含任何的模式串 建立Trie图,求得每个节点是否是不可被包含的串,然后进行DP dp[i][ ...
- hdu 3236 二维背包
明天来一发 hdu 4501 算是这题的简化版吧