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)

这样就可以过啦


代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; const int N = 300005;
const int P = 20071027; struct trie{
trie *ch[26];
int flag;
void clear(){
flag=0;
for(int i=0;i<26;i++) ch[i]=NULL;
}
}pool[105*4000],*root;
int cnt; int d[N];
void add(){
char s[105];
scanf("%s",s);
int len=strlen(s),id;
trie *p=root;
for(int i=0;i<len;i++){
id=s[i]-'a';
if(!p->ch[id]){
pool[++cnt].clear();
p->ch[id]=&pool[cnt];
}
p=p->ch[id];
}
p->flag++;
} char sh[N]; int main()
{
int len,n,i,j,kase=0;
trie *p;
root=&pool[++cnt];
while(scanf("%s",sh)!=EOF){
len=strlen(sh);
scanf("%d",&n);
pool[1].clear(); cnt=1;
for(i=0;i<n;i++) add();
d[len]=1;
for(i=len-1;i>=0;i--){
p=root;
d[i]=0;
for(j=i;j<len;j++){
if(p->ch[sh[j]-'a']) {
p=p->ch[sh[j]-'a'];
if(p->flag)
d[i]=(d[i]+d[j+1])%P;
}
else break;
}
}
printf("Case %d: %d\n",++kase,d[0]);
} return 0;
}

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. vue-learning:16 - js - computed

    computed 在指令章节讲过,插值{{ }}和指令都接受变量和表达式的写法,使用表达式可以进行简单的二元或三元运算.但如果要执行更加复杂的计算或频繁重复的计算,如果还是直接写在指令的表达式中会让代 ...

  2. 【36.11%】【codeforces 725C】Hidden Word

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【2016福建省夏令营Day1】数据结构

    Problem 1 楼房(build.cpp/c/pas) [题目描述] 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i ...

  4. HDU1166 敌兵布阵 BZOJ1012 最大数[树状数组]

    一.前置知识-树状数组 树状数组(binary indexed tree)是一种简洁的代码量很小的数据结构,能够高效的处理前缀区间上的问题.在很多情况下能写树状数组解决的就不用码半天线段树了. 树状数 ...

  5. 以windows服务方式快速部署免安装版Postgres数据库

    目录 以windows服务方式快速部署免安装版Postgres数据库 1.下载Postgresql数据库免安装包 2.安装环境准备及验证 解压文件 测试环境依赖 3.创建并初始化数据目录 创建数据目录 ...

  6. 激励函数 (Activation)

    softplus是有关概率的巴拉巴拉? Torch 中的激励函数有很多, 不过我们平时要用到的就这几个. relu, sigmoid, tanh, softplus. 那我们就看看他们各自长什么样啦. ...

  7. 【elasticsearch】数据早8小时Or晚8小时,你知道为什么吗,附解决方案

    前言 这篇文章,不会解释什么是本初子午线,只想以做实验的方式来理解数据差8小时的问题.下面就先说结论,再来谈原理. 解决方案 想必大家都很清楚:中国标准时间= UTC + 8小时. 那么所有和时区有关 ...

  8. 如何基于TencentOS tiny,快速打造属于自己的IoT小应用?

    导语 | 近日,云+社区技术沙龙“腾讯开源技术”圆满落幕.本次沙龙邀请了多位腾讯技术专家,围绕腾讯开源与众多开发者进行探讨,深度揭秘了腾讯开源项目TencentOS tiny.TubeMQ.Kona  ...

  9. Jquery为动态添加的元素添加事件

    $("tbody").on("click","button", function() { var text = $(this).parent ...

  10. 牛客训练赛55 E 树

    很妙的一个树形DP问题,简单考虑了一下就过了 https://ac.nowcoder.com/acm/contest/2927/E 主要就是推公式(公式有点长呀) 大概就是这样,其实挺简单的. #in ...