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) ...
随机推荐
- lazyload
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Spring面向切面编程(AOP)
1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...
- 访问javaweb服务器sessionid存放问题
sessionid存放在cookies里面,key是JSESSIONID
- strlen与sizeof
strlen计算不包括终止符null字节的字符串长度,而sizeof则计算包括终止null字节的长度.另一个差别,strlen需要一次函数调用,而sizeof在编译时计算缓冲区长度.
- alias命令(使用命令别名)
通过alias命令可以给一些命令定义别名,如,将长的难记住的命令起一个容易记住的别名,提高工作效率 alias -p 查看已有的别名列表 命名别名格式: alias 新命令名='原命令名 -参数/选项 ...
- 超好用的plsql设置
http://blog.itpub.net/24496241/viewspace-740917/
- ReentrantLock和synchronized两种锁定机制
ReentrantLock和synchronized两种锁定机制 >>应用synchronized同步锁 把代码块声明为 synchronized,使得该代码具有 原子性(atomicit ...
- bluetooth service uuid
转自:https://www.bluetooth.com/specifications/assigned-numbers/service-discovery service discovery ...
- Delphi中的异常处理
转载:http://www.cnblogs.com/doit8791/archive/2012/05/08/2489471.html 以前写Delphi程序一直不注意异常处理,对其异常处理的机制总是一 ...
- 【Java EE 学习 21 上】【其它类型的监听器】【使用HttpSessionActivationListener监听session的活化和钝化】
一.ServletContextListener Method Summary void contextDestroyed(ServletContextEvent sce) R ...