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+ ...
随机推荐
- jedis入门实例
在使用传统的关系数据库,我们都需要依赖一个所谓的实现了jdbc规范的驱动程序来连接数据库,这些驱动程序由各大数据库厂商提供.这些驱动就是jar包,里面就是封装了对数据库的通信协议,我们通过简单的调用就 ...
- 一个使用微软Azure blob实现文件下载功能的实例-附带源文件
Running the sample Please follow the steps below. Step 1: Open the CSAzureServeFilesFromBlobStorage. ...
- MapReduce的执行过程.
作业在运行时,数据或者是作业调用的一个运行图. 用户写的代码通过JobClient提交给JobTracker Job对象中封装了JobClient JobConf和我们的Job对象几乎是一回事. 把我 ...
- String比较
String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc&quo ...
- OpenStack Cinder组件支持的块存储设备表
摘自恒天云官网:http://www.hengtianyun.com/download-show-id-18.html OpenStack的Cinder组件底层可以连接多种存储设备和方案,每一个Ope ...
- AndroidAsync :异步Socket,http(client+server),websocket和socket.io的Android类库
AndroidAsync是一个用于Android应用的异步Socket,http(client+server),websocket和socket.io的类库.基于NIO,没有线程.它使用java.ni ...
- DB2 VALUES用法详解
都知道Oracle有一个虚表(dual),我们可以用select sysdate from dual获取寄存器中的值.在DB2中,可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄 ...
- CodeForces 176B Word Cut (计数DP)
Word Cut Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- Spring Auto-Wiring Beans with @Autowired annotation
In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...
- [iOS 多线程 & 网络 - 3.0] - 在线动画Demo
A.需求 所有数据都从服务器下载 动画列表包含:图片.动画名标题.时长副标题 点击打开动画观看 code source: https://github.com/hellovoidworld/Vid ...