UVA 1401 - Remember the Word

[题目链接]

题意:给定一些单词。和一个长串。问这个长串拆分成已有单词,能拆分成几种方式

思路:Trie,先把单词建成Trie。然后进行dp。dp[i]表示以i为开头的情况,然后每一个状态仅仅要在Trie树上找到对应的i开头的单词,然后dp[i] = sum{dp[i + len]}进行状态转移就可以

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. const int MOD = 20071027;
  5. const int N = 300005;
  6.  
  7. char str[N], word[105];
  8. int S, sz;
  9.  
  10. struct Node {
  11. int next[26], val;
  12. } node[N];
  13.  
  14. void insert(char *str) {
  15. int len = strlen(str);
  16. int u = 0;
  17. for (int i = 0; i < len; i++) {
  18. int v = str[i] - 'a';
  19. if (node[u].next[v])
  20. u = node[u].next[v];
  21. else {
  22. node[u].next[v] = sz++;
  23. u = node[u].next[v];
  24. node[u].val = 0;
  25. memset(node[u].next, 0, sizeof(node[u].next));
  26. }
  27. }
  28. node[u].val = 1;
  29. }
  30.  
  31. void init() {
  32. sz = 1;
  33. memset(node[0].next, 0, sizeof(node[0].next));
  34. node[0].val = 0;
  35. }
  36.  
  37. int dp[N];
  38.  
  39. void find(int i) {
  40. int u = 0;
  41. dp[i] = 0;
  42. for (int j = i; str[j]; j++) {
  43. int v = str[j] - 'a';
  44. if (!node[u].next[v]) return;
  45. u = node[u].next[v];
  46. if (node[u].val) dp[i] = (dp[i] + dp[j + 1]) % MOD;
  47. }
  48. }
  49.  
  50. int main() {
  51. int cas = 0;
  52. while (~scanf("%s", str)) {
  53. scanf("%d", &S);
  54. init();
  55. while (S--) {
  56. scanf("%s", word);
  57. insert(word);
  58. }
  59. int len = strlen(str);
  60. dp[len] = 1;
  61. for (int i = len - 1; i >= 0; i--)
  62. find(i);
  63. printf("Case %d: %d\n", ++cas, dp[0]);
  64. }
  65. return 0;
  66. }

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. Mybatis拦截器介绍

    拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...

  2. Tableau学习笔记之二

    2张图片解析下Tableau 9.0界面的功能 1.数据加载界面: 2.数据分析界面:

  3. HDU 5607 graph(DP+矩阵乘法)

    [题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...

  4. Intellij IDEA,WebStorm-keymap(转)

    1. ctrl + shift + n: 打开工程中的文件2. ctrl + j: 输出模板3. ctrl + b: 跳到变量申明处4. ctrl + alt + T: 围绕包裹代码(包括zencod ...

  5. Min Edit Distance

    Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...

  6. Redis3.0 Install

    Installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redi ...

  7. storm的安装配置

    一.安装Zookeeper 1.设置.profile文件: export ZOOKEEPER_HOME=/home/hadoop/streamdata/zookeeper-3.4.5-cdh4.5.0 ...

  8. 分类算法之朴素贝叶斯分类(Naive Bayesian Classification)

    1.什么是分类 分类是一种重要的数据分析形式,它提取刻画重要数据类的模型.这种模型称为分类器,预测分类的(离散的,无序的)类标号.例如医生对病人进行诊断是一个典型的分类过程,医生不是一眼就看出病人得了 ...

  9. 树莓派I2C连接18B20

    按图连接设备 载入模块 sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ 显示结果 ls pi@raspberr ...

  10. 用Intellij Idea创建简单的Servlet

    Servlet作为Java服务端程序,使用起来还是挺方便的,下面是具体配置过程,我用的是Intellij Idea. 1. 做好必要准备,Intellij Idea(或者Eclipse for J2E ...