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)的更多相关文章

  1. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  2. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  3. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  4. UVA 1401 Remember the Word

    字典树优化DP                                Remember the Word Time Limit: 3000MS   Memory Limit: Unknown ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 【UVA1401】Remember the Word Trie+dp

    题目大意:给定一个字符串和一个字符串集合,问从集合中选出若干个串组成给定母串的不同方案数. 题解:有些类似于背包问题.状态很好表示,为:\(dp[i]\) 表示母串前 i 个字符的不同方案数,因此,有 ...

  7. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  8. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

  9. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

随机推荐

  1. .net-C#代码判断

    ylbtech-doc:.net-C#代码判断 C#代码判断 1.A,C#代码判断返回顶部 01.{ C#题目}public static void Main(string[] args){     ...

  2. 【剑指offer 面试题16】反转链表

    思路: 用三个指针preNode.curNode.nextNode完成. #include <iostream> using namespace std; struct ListNode ...

  3. linux常用命令之--文件打包与压缩命令

    linux的文件打包与压缩命令 1.压缩与解压命令 compress:用于压缩指定的文件,后缀为.z 其命令格式如下: compress [-d] 文件名 常用参数: -d:解压被压缩的文件(.z为后 ...

  4. 如何用Entity Framework 6 连接Sqlite数据库[转]

    获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...

  5. 【windows核心编程】IO完成端口(IOCP)复制文件小例前简单说明

    1.关于IOCP IOCP即IO完成端口,是一种高伸缩高效率的异步IO方式,一个设备或文件与一个IO完成端口相关联,当文件或设备的异步IO操作完成的时候,去IO完成端口的[完成队列]取一项,根据完成键 ...

  6. JQuery插件的学习

    此前一直想就关于Jquery插件的开发,做一个深入的学习,由于各种原因,当然主要是自己太懒了...今天就系统分析一下Jquery插件的开发(参考了http://www.xprogrammer.com/ ...

  7. Ajax异步请求PHP数据

    来源:http://www.ido321.com/1138.html 接到了老师的一个作业,实现的布局如图: 如果输入了科室ID,科室名字只显示与ID对应的,若没有输入,则显示全部,然后根据I科室名字 ...

  8. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...

  9. warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

    最近在编译库文件后,使用它做APP,遇到如下问题: 1>LIBCMT.lib(invarg.obj) : error LNK2005: __pInvalidArgHandler 已经在 LIBC ...

  10. NetAddr

    http://www.searchdatabase.com.cn/showcontent_66349.htm   [techTarget中国,其专注于IT领域企业级高端市场,为IT专业技术人员和管理决 ...