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. POJ 1195- Mobile phones(二维BIT)

    题意: 矩阵上的单点更新,范围求和 #include <map> #include <set> #include <list> #include <cmath ...

  2. 【LR】录制测试脚本中的基本菜单

    学习来源: MBoo,小强老师性能测试及Loadrunner培训  ——录制测试脚本: 1.Vuser -> run-time settings ->General Run Logic : ...

  3. [算法] 冒泡排序 Bubble Sort

    冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  4. jsp文件上传、下载

    一.文件上传 上传文件是Web开发中经常要用到的功能:例如在基于B/S的人事信息管理系统中上传照片,在新闻发布系统中上传图片等等.....要实现文件上传功能,就需要综合利用java中的文件输入和输出相 ...

  5. 如何使用git创建远程仓库(供局域网多人使用)

    用git init(默认创建的是私人的仓库)创建的仓库,推送是不会成功的. 因此在git server端,我们要用 git --bare init --shared=group 来创建一个bare库, ...

  6. RHEL6.4 KVM 桥接上网的设置

    关闭网络管理器 chkconfig NetworkManager off  ##和桥接有冲突,要关闭 service NetworkManager stop   修改eth0为物理网口,br0为桥接网 ...

  7. Java中快如闪电的线程间通讯

    这个故事源自一个很简单的想法:创建一个对开发人员友好的.简单轻量的线程间通讯框架,完全不用锁.同步器.信号量.等待和通知,在Java里开发一个轻量.无锁的线程内通讯框架:并且也没有队列.消息.事件或任 ...

  8. unix 时间

    from_unixtime()是MySQL里的时间函数 mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' ) ->20071120 mysq ...

  9. HDU 5724 Chess (sg函数)

    Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...

  10. CodeForces 489A SwapSort (选择排序法)

    SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...