题意:白书P209

本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星)

dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp[i+slen(j)],如果后缀j存在符合前缀的方案,slen(j)为该后缀完全匹配前缀的长度}

这种利用前缀进行dp从后往前推的方法我并没有用过,学习了

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<string>
  6. using namespace std;
  7. const int MAXN = 2e6+11;
  8. const int MOD = 20071027;
  9. char str[MAXN>>1],s[MAXN>>1];
  10. int dp[MAXN>>1];
  11. struct Trie{
  12. int son[MAXN],bro[MAXN],tot,root;
  13. char val[MAXN];
  14. int sz[MAXN];
  15. int isword[MAXN];
  16. void init(){
  17. memset(son,-1,sizeof son);
  18. root=0;tot=1;
  19. val[root]=0;sz[root]=0;isword[root]=0;
  20. }
  21. int node(int fa,char ch){
  22. bro[tot]=son[fa]; son[fa]=tot;//yuanlaide diyige zuoerzi bianwei youxiongdi
  23. val[tot]=ch; sz[tot]=0; isword[tot]=0; // son[tot]=-1;
  24. return tot++;
  25. }
  26. void insert(char str[]){
  27. int now=0,lc=-1;
  28. for(int i=0;str[i];i++){
  29. bool flag=0;
  30. for(lc=son[now];~lc;lc=bro[lc]){
  31. if(val[lc]==str[i]){
  32. flag=1;break;
  33. }
  34. }
  35. if(!flag) lc=node(now,str[i]);
  36. now=lc;sz[now]++;
  37. }
  38. isword[now]++;
  39. }
  40. int find(int from,int to){
  41. int now=0,lc=-1,ans=0;
  42. for(int i=from;i<to;i++){
  43. bool flag=0;
  44. for(lc=son[now];~lc;lc=bro[lc]){
  45. if(val[lc]==str[i]){
  46. flag=1;
  47. if(isword[lc]) ans=(ans+dp[i+1])%MOD;
  48. now=lc;
  49. break;
  50. }
  51. }
  52. if(!flag) break;
  53. }
  54. return ans;
  55. }
  56. }trie;
  57. int main(){
  58. int kase=0;
  59. while(~scanf("%s",str)){
  60. trie.init();
  61. int n; scanf("%d",&n);
  62. for(int i=1;i<=n;i++){
  63. scanf("%s",s);
  64. trie.insert(s);
  65. }
  66. int len=strlen(str);
  67. memset(dp,0,sizeof dp);
  68. dp[len]=1;//!!!
  69. for(int i=len-1;i>=0;i--){
  70. int tmp=trie.find(i,len);
  71. dp[i]=tmp;
  72. }
  73. printf("Case %d: %d\n",++kase,dp[0]);
  74. }
  75. return 0;
  76. }

UVALive - 3942 左儿子trie DP的更多相关文章

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

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

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

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

  3. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  4. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

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

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

  6. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  7. Vijos p1518 河流 转二叉树左儿子又兄弟

    左儿子又兄弟的转发一定要掌握啊,竞赛必用,主要是降低编程复杂度,省时间.个人觉得状压DP也是为了降低编程复杂度. 方程就不说了,程序应该能看得懂,用的记忆化搜索,方便理解. #include<c ...

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

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

  9. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

随机推荐

  1. 1、 Shiro框架:认证,授权(验权 2. Shiro框架实现权限控制方式:

    1. Shiro框架:认证,授权(验权) a) 认证逻辑:applicationCode—>通过工具类获取subject对象,调用login方法参数令牌信息->安全管理器------> ...

  2. 僵固式思维 OR 成长式思维

    有意无意中,看到这样的一篇文章,觉得非常富有正能量,而且也比较有同感.而且,不仅仅对于职场暂时失落或者失意的人有帮助,就是对学生,也一样的.故特分享,以共勉之. 我想每个新人进入职场之后都会遇到的第一 ...

  3. Python基础-3

    目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...

  4. C#获取文件的Content-Type(MIME Type)的方法

    使用静态类MimeMapping(需要.NET Framework 4.5及以后的支持) string fileName = "D:\myfile.txt"; var conten ...

  5. css总结12:CSS Float(浮动)

    1 定义:CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. 2 解释:浮动的元素,浮在界面上,脱离了HTML文本流. 元素的水平方向浮动,意味着元素只能左右移动而不能 ...

  6. HDU 4803 Poor Warehouse Keeper(贪心)

    题目链接 题意 :屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,1+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的 ...

  7. 20169219 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 一.程序设计中临时变量的使用 任务: //定义一个数组 int arr[] = {1,2,3,4,5,6,7,8}; //打印原始数组的值 for(int i:arr ...

  8. 关于Java异常一段很有意思的代码

    今天学习了Java的异常,讲到try-catch-finally时,老师演示了一段代码,觉得很有意思,很能反映出其执行的过程,让自己有点绕,特意记录一下. 只要代码执行到try代码内部, 不管有没有异 ...

  9. centos 重新安装python3.6之后 yum 无法使用报错

    问题: $ yum File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid ...

  10. IOS 防坑指南

    1. 读写文件 1. IOS 8 中  stringWithContentsOfFile 已被移除 2. 创建文件必须放到 应用下 Documents 下面 // // FileHelper.swif ...