UvaLive3942(Trie + dp)
查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了。
类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置了,省去了很多不必要状态。复杂度就O(n*Trie)。
终于比刘汝佳的代码优雅了(弥天大雾)
const int maxn = 3e5 + ;
const int maxnode = * + ;
const int mod = ; int ch[maxnode][]; struct Trie {
int sz;
bool IsWord[maxnode]; Trie() {
sz = ;
init(ch[], );
} void insert(char *t) {
int len = strlen(t), u = ;
for (int i = ; i < len; ++i) {
if (!ch[u][t[i] - 'a']) {
init(ch[sz], );
IsWord[sz] = false;
ch[u][t[i] - 'a'] = sz++;
}
u = ch[u][t[i] - 'a'];
}
IsWord[u] = true;
} int find(int pos, char *str, int *dp) {
int ret = , u = ;
for (int i = pos; str[i]; ++i) {
if (!ch[u][str[i] - 'a']) return ret;
u = ch[u][str[i] - 'a'];
if (IsWord[u]) ret = ((ll)ret + dp[i + ]) % mod;
}
return ret;
}
}; char str[maxn], t[];
int S, L, dp[maxn], kase; int main() {
while (~scanf("%s", str)) {
Trie T;
for (scanf("%d", &S); S; S--) {
scanf("%s", t);
T.insert(t);
}
L = strlen(str);
dp[L] = ;
for (int i = L - ; ~i; --i) {
dp[i] = T.find(i, str, dp);
}
printf("Case %d: %d\n", ++kase, dp[]);
}
return ;
}
UvaLive3942(Trie + dp)的更多相关文章
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- ACdreamOJ 1154 Lowbit Sum (数字dp)
ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...
- 【Luogu3732】[HAOI2017]供给侧改革(Trie树)
[Luogu3732][HAOI2017]供给侧改革(Trie树) 题面 洛谷 给定一个纯随机的\(01\)串,每次询问\([L,R]\)之间所有后缀两两之间的\(LCP\)的最大值. 题解 一个暴力 ...
- 「SDOI2016」储能表(数位dp)
「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ4712】洪水(动态dp)
[BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...
随机推荐
- ditaa - 把ascii图形转成图片
ditaa ditaa是一个把ascii图形转成图片的工具. 在查看zguide时看到这个文档是用gitdown生成的.zguide文档格式排版非常不错,以后要抽时间好好学习一下. 每章写一个txt文 ...
- Android - 监听Activity点击无效
监听Activity点击无效 本文地址: http://blog.csdn.net/caroline_wendy Activity须要先在Manifest注冊,才干在app中使用; Manifest: ...
- iOS 各种编译错误汇总
1.error: macro names must be identifiers YourProject_prefix.pch 原因: 因为你弄脏了预处理器宏,在它处于<Multiple Val ...
- Why there are no job running on hadoop
Using hadoop1.3.0. I ran the example WordCount correctly in eclipse. But when I enter localhost:5003 ...
- HTML 客户端存储
在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...
- C# delegate Action<T> lambda表达式
转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...
- fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400); % Run fminunc to obtain the optimal theta% This ...
- mysql优化----explain的列分析
sql语句优化: : sql语句的时间花在哪儿? 答: 等待时间 , 执行时间. 等待时间:看是不是被锁住了,那就不是语句层面了是服务端层面了,看连接数内存. 执行时间:到底取出多少行,一次性取出1万 ...
- redis09---redis 服务器端命令
redis 服务器端命令 db0,db1,db2是数据库,外层是服务器,服务器下面有20个数据库. :>time ) "" //多少秒 ) "" //多少 ...
- 数据结构之 排序---折半插入排序(时间复杂度 O(nlog2 n) )
排序 Time Limit: 1000MS Memory limit: 32678K 题目描述 给你N(N<=100)个数,请你按照从小到大的顺序输出. 输入 输入数据第一行是一个正整数N,第二 ...