递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的前缀,且x可以和某个单词匹配。判断后缀串S[i...n]能和哪些单词匹配,使用字典树来实现O(100)复杂度判断。

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. const int MOD = ;
  4. const int maxnode = * + ;
  5. const int sigma_size = ;
  6. const int maxn = + ;
  7.  
  8. char S[maxn], sb[ + ];
  9. int d[maxn];
  10.  
  11. struct Trie {
  12. int ch[maxnode][sigma_size];
  13. int val[maxnode];
  14. int sz;
  15. void clear() {
  16. sz = ;
  17. memset(ch[], , sizeof(ch[]));
  18. }
  19. int idx(char c) {
  20. return c - 'a';
  21. }
  22.  
  23. void insert(char *s) {
  24. int u = , n = strlen(s);
  25. for(int i = ; i < n; i++) {
  26. int c = idx(s[i]);
  27. if(!ch[u][c]) {
  28. val[sz] = ;
  29. memset(ch[sz], , sizeof(ch[sz]));
  30. ch[u][c] = sz++;
  31. }
  32. u = ch[u][c];
  33. }
  34. val[u] = n;
  35. }
  36.  
  37. void query_prefix(char *s, int i, int *d) {
  38. int u = ;
  39. for(int j = ; s[j] != '\0'; j++) {
  40. int c = idx(s[j]);
  41. if(!ch[u][c]) break;
  42. u = ch[u][c];
  43. if(val[u]) {
  44. d[i] = (d[i] + d[i + val[u]]) % MOD;
  45. }
  46. }
  47. }
  48. };
  49.  
  50. Trie tree;
  51.  
  52. int main() {
  53. int m, kase = ;
  54. while(scanf("%s", S) == ) {
  55. tree.clear();
  56. scanf("%d", &m);
  57. for(int i = ; i < m; i++) {
  58. scanf("%s", sb);
  59. tree.insert(sb);
  60. }
  61. int n = strlen(S);
  62. d[n] = ;
  63. for(int i = n-; i >= ; i--) {
  64. d[i] = ;
  65. tree.query_prefix(S+i, i, d);
  66. }
  67. printf("Case %d: %d\n", kase++, d[]);
  68. }
  69. return ;
  70. }

如有不当之处欢迎指出!

UVALive - 3942 (字典树)的更多相关文章

  1. UVALive 3942 字典树+dp

    其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...

  2. UVALive 5029 字典树

    E - Encoded Barcodes Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB    6 ...

  3. UVALive 5913 字典树

    先输入n个字符串的字典,每个字符串的前缀+后缀可以组成新的合法字符串,但肯定是有重复的,问从给定的字符串,生成的所有可能的字符串为多少个 把前缀和后缀压入字典树,达到前缀和后缀的去重,首先的总和即为前 ...

  4. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  5. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...

  7. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  8. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. Trie(字典树)解析及其在编程竞赛中的典型应用举例

    摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...

随机推荐

  1. fastjson JSONObject遍历

    private static String getDesc(String jsonStr, String key) { JSONObject jsonObject = JSONObject.parse ...

  2. js比较两个单独的数组或对象是否相等

    所谓js的中的传值,其实也就是说5种基本数据类型(null,undefind,boolean,number,string) 传引用也就是说的那个引用数据类型,(array和object) 基本数据类型 ...

  3. ESP8266 RTOS SDK烧写环境构建

    简介 esptool是一个Python软件程序,适用于ESP8266等一系列芯片的烧写,灵活高效. 环境构建 在官网下载安装最新2.7版python (linux和os x一般会自带python2.7 ...

  4. [INS-06006] Passwordless SSH connectivity not set up between the following node(s)

    解决方法1 参考:11.2.0.4 runInstaller: [INS-06006] Passwordless SSH connectivity not set up between the fol ...

  5. 白话skynet第二篇:skynet的通信调试pack和sprotol

    今天来说说Skynet客户端和服务端网络通信的基础部分. Skynet当前版本.lua是skynet自带的5.3版本. 根据示例,我们可以知道.通信的步骤如下. 客户端按大小端打包成二进制. sock ...

  6. 68.jq---tab选项实现网页定点切换

    {volist name="list" id="vo"}<div class="nav_div" style="positi ...

  7. docker 标记和推送镜像

    打开Launchpad并定位到docker Quickstart Terminal图标. 点击Docker Quickstart Terminal图标, 打开一个窗口. 将光标定位到Docker Qu ...

  8. UGUI动态更换精灵图片

    //动态更换精灵图片 m_headimage.overrideSprite = Resources.Load("texture/"+info.HeadPortrait,typeof ...

  9. 18.12.02-C语言练习:韩信点兵

    C语言练习:韩信点兵 题目说明:本题是中国经典问题,有多种解法,从数论课程角度看,是一个不定方程组,而且答案不唯一. 但这里采用程序解法,使用的是暴力破解.枚举可能的解,然后根据条件判断,满足所有条件 ...

  10. Fastcgi、CGI 是什么

    1.CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. 2.web server(比如说nginx)只是内容的分发者. 比如,如果请求/index ...