UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word
[题目链接]
题意:给定一些单词。和一个长串。问这个长串拆分成已有单词,能拆分成几种方式
思路:Trie,先把单词建成Trie。然后进行dp。dp[i]表示以i为开头的情况,然后每一个状态仅仅要在Trie树上找到对应的i开头的单词,然后dp[i] = sum{dp[i + len]}进行状态转移就可以
代码:
- #include <cstdio>
- #include <cstring>
- const int MOD = 20071027;
- const int N = 300005;
- char str[N], word[105];
- int S, sz;
- struct Node {
- int next[26], val;
- } node[N];
- void insert(char *str) {
- int len = strlen(str);
- int u = 0;
- for (int i = 0; i < len; i++) {
- int v = str[i] - 'a';
- if (node[u].next[v])
- u = node[u].next[v];
- else {
- node[u].next[v] = sz++;
- u = node[u].next[v];
- node[u].val = 0;
- memset(node[u].next, 0, sizeof(node[u].next));
- }
- }
- node[u].val = 1;
- }
- void init() {
- sz = 1;
- memset(node[0].next, 0, sizeof(node[0].next));
- node[0].val = 0;
- }
- int dp[N];
- void find(int i) {
- int u = 0;
- dp[i] = 0;
- for (int j = i; str[j]; j++) {
- int v = str[j] - 'a';
- if (!node[u].next[v]) return;
- u = node[u].next[v];
- if (node[u].val) dp[i] = (dp[i] + dp[j + 1]) % MOD;
- }
- }
- int main() {
- int cas = 0;
- while (~scanf("%s", str)) {
- scanf("%d", &S);
- init();
- while (S--) {
- scanf("%s", word);
- insert(word);
- }
- int len = strlen(str);
- dp[len] = 1;
- for (int i = len - 1; i >= 0; i--)
- find(i);
- printf("Case %d: %d\n", ++cas, dp[0]);
- }
- return 0;
- }
UVA 1401 - Remember the Word(Trie+DP)的更多相关文章
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...
- UVA 1401 Remember the Word(用Trie加速动态规划)
Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- UVA 1401 Remember the Word
字典树优化DP Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【UVA1401】Remember the Word Trie+dp
题目大意:给定一个字符串和一个字符串集合,问从集合中选出若干个串组成给定母串的不同方案数. 题解:有些类似于背包问题.状态很好表示,为:\(dp[i]\) 表示母串前 i 个字符的不同方案数,因此,有 ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
随机推荐
- Mybatis拦截器介绍
拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...
- Tableau学习笔记之二
2张图片解析下Tableau 9.0界面的功能 1.数据加载界面: 2.数据分析界面:
- HDU 5607 graph(DP+矩阵乘法)
[题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...
- Intellij IDEA,WebStorm-keymap(转)
1. ctrl + shift + n: 打开工程中的文件2. ctrl + j: 输出模板3. ctrl + b: 跳到变量申明处4. ctrl + alt + T: 围绕包裹代码(包括zencod ...
- Min Edit Distance
Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...
- Redis3.0 Install
Installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redi ...
- storm的安装配置
一.安装Zookeeper 1.设置.profile文件: export ZOOKEEPER_HOME=/home/hadoop/streamdata/zookeeper-3.4.5-cdh4.5.0 ...
- 分类算法之朴素贝叶斯分类(Naive Bayesian Classification)
1.什么是分类 分类是一种重要的数据分析形式,它提取刻画重要数据类的模型.这种模型称为分类器,预测分类的(离散的,无序的)类标号.例如医生对病人进行诊断是一个典型的分类过程,医生不是一眼就看出病人得了 ...
- 树莓派I2C连接18B20
按图连接设备 载入模块 sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ 显示结果 ls pi@raspberr ...
- 用Intellij Idea创建简单的Servlet
Servlet作为Java服务端程序,使用起来还是挺方便的,下面是具体配置过程,我用的是Intellij Idea. 1. 做好必要准备,Intellij Idea(或者Eclipse for J2E ...