F - Remember the Word

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-03-07)

Description

 

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.

Since Jiejie can't remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.

Input

The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

The second line contains an integer S<tex2html_verbatim_mark> , 1S4000<tex2html_verbatim_mark> .

Each of the following S<tex2html_verbatim_mark> lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

There is a blank line between consecutive test cases.

You should proceed to the end of file.

Output

For each test case, output the number, as described above, from the task description modulo 20071027.

Sample Input

  1. abcd
  2. 4
  3. a
  4. b
  5. cd
  6. ab

Sample Output

  1. Case 1: 2
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. int ch[][],val[];
  6. int sz,d[];
  7. int idx(char c)
  8. {
  9. return c-'a';
  10. }
  11. void insert(char *s,int v)
  12. {
  13. int u=,n=strlen(s);
  14. for(int i=;i<n;i++)
  15. {
  16. int c=idx(s[i]);
  17. if(!ch[u][c])
  18. {
  19. memset(ch[sz],,sizeof(ch[sz]));
  20. val[sz]=;
  21. ch[u][c]=sz++;
  22. }
  23. u=ch[u][c];
  24. }
  25. val[u]=v;
  26. }
  27. int main()
  28. {
  29. char s[],a[];
  30. int i,x,t=,j;
  31. while(~scanf("%s",s))
  32. {
  33. memset(d,,sizeof(d));
  34. sz=;
  35. memset(ch[],,sizeof(ch[]));
  36. scanf("%d",&x);
  37. for(i=;i<=x;i++)
  38. {
  39. scanf("%s",a);
  40. insert(a,-);
  41. }
  42. int root,i,j,k;
  43. int len=strlen(s);
  44. d[len]=;
  45. for(i=len-;i>=;i--)
  46. {
  47. root=;
  48. for(j=,k=i;k<len;j++,k++)
  49. {
  50. int c=idx(s[k]);
  51. if(ch[root][c]==)
  52. break;
  53. else if(val[ch[root][c]]==-)
  54. {
  55. d[i] += d[i+j+];
  56. if(d[i] >= ) d[i] %= ;
  57. }
  58. root=ch[root][c];
  59. }
  60.  
  61. }
  62. printf("Case %d: %d\n",t++,d[]%);
  63. }
  64. }

大白书 209 remember the word的更多相关文章

  1. 大白书中无向图的点双联通分量(BCC)模板的分析与理解

    对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...

  2. DAG 上的动态规划(训练指南—大白书)

    有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.矩形嵌套 题目描述:       ...

  3. UVALive 3942 Remember the Word

    题意:给出一个由S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复 使用),有多少种方法? Sample Input abcd 4 a b cd ab Sample ...

  4. la3523 白书例题 圆桌骑士 双联通分量+二分图

    具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include & ...

  5. C#操作Word的超详细总结 ---转载

    C#操作Word的超详细总结 本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设 ...

  6. Uva12206 Stammering Aliens 后缀数组&&Hash

    Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...

  7. 【JSOI2007】麻将 bzoj 1028

    Description 麻 将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西.北.中.发.白七种)和序数牌(分为条子.饼子.万子三种花色,每种花色各有一到 九的九种牌),每种牌各四张 ...

  8. poj2284 欧拉公式

    题意:给出一图形,求该图形把平面分成了几部分 欧拉公式: http://blog.csdn.net/wangxiaojun911/article/details/4586550 对于二维平面上的情况. ...

  9. UVA11426 欧拉函数

    大白书P125 #include <iostream> #include <cstring> using namespace std; #define MMX 4000010 ...

随机推荐

  1. 线段树(单点更新)HDU1166、HDU1742

    在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...

  2. python 在调用时计算默认值

    大家都知道python的默认值是在函数定义时计算出来的, 也就是说默认值只会计算一次, 之后函数调用时, 如果参数没有给出,同一个值会赋值给变量, 这会导致, 如果我们想要一个list默认值, 新手通 ...

  3. C++ 引用(&)

    #include <iostream> void sort(int &a, int &b){ if (a>=b) { return; } if (a<b) { ...

  4. linux采用模块方法,添加一个新的设备

    该文转载自:http://rangercyh.blog.51cto.com/1444712/521244 系统调用是操作系统内核和应用程序之间的接口,而设备驱动程序是操作系统内核和机器硬件之间的接口. ...

  5. Entity SQL 初入

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  6. 程序员的编辑器——VIM

    from:http://blog.chinaunix.net/uid-11278770-id-148579.html Chinaunix首页 | 论坛 | 认证专区 | 博客 登录 | 注册      ...

  7. linux扩展lvm磁盘

    env: centos 6.5 x64 hyper-v虚拟机 这个方法可以在当前运行的系统中扩展root磁盘 详细步骤 之前想创建的一个虚拟机的磁盘空间不够用了,所以想扩容一下磁盘. 正好使用的时候是 ...

  8. linux 下C语言编程库文件处理与Makefile编写

    做开发快3年了,在linux下编译安装软件算是家常便饭了.就拿gcc来说,都有不下10次了,可基本每次都会碰到些奇奇怪怪的问题.看来还是像vs.codeblocks这样的ide把人弄蠢了.便下定决心一 ...

  9. Laravel 5.1 事件、事件监听的简单应用

    ​ 有时候当我们单纯的看 Laravel 手册的时候会有一些疑惑,比如说系统服务下的授权和事件,这些功能服务的应用场景是什么,其实如果没有经历过一定的开发经验有这些疑惑是很正常的事情,但是当我们在工作 ...

  10. git之环境配置(window+git+github)

    本地安装git 下载最新版的git:https://msysgit.googlecode.com/files/Git-1.9.0-preview20140217.exe 安装步骤:http://jin ...