大白书 209 remember the word
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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> , 1
S
4000<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
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int ch[][],val[];
int sz,d[];
int idx(char c)
{
return c-'a';
}
void insert(char *s,int v)
{
int u=,n=strlen(s);
for(int i=;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
int main()
{
char s[],a[];
int i,x,t=,j;
while(~scanf("%s",s))
{
memset(d,,sizeof(d));
sz=;
memset(ch[],,sizeof(ch[]));
scanf("%d",&x);
for(i=;i<=x;i++)
{
scanf("%s",a);
insert(a,-);
}
int root,i,j,k;
int len=strlen(s);
d[len]=;
for(i=len-;i>=;i--)
{
root=;
for(j=,k=i;k<len;j++,k++)
{
int c=idx(s[k]);
if(ch[root][c]==)
break;
else if(val[ch[root][c]]==-)
{
d[i] += d[i+j+];
if(d[i] >= ) d[i] %= ;
}
root=ch[root][c];
} }
printf("Case %d: %d\n",t++,d[]%);
}
}
大白书 209 remember the word的更多相关文章
- 大白书中无向图的点双联通分量(BCC)模板的分析与理解
对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...
- DAG 上的动态规划(训练指南—大白书)
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.矩形嵌套 题目描述: ...
- UVALive 3942 Remember the Word
题意:给出一个由S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复 使用),有多少种方法? Sample Input abcd 4 a b cd ab Sample ...
- la3523 白书例题 圆桌骑士 双联通分量+二分图
具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include & ...
- C#操作Word的超详细总结 ---转载
C#操作Word的超详细总结 本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设 ...
- Uva12206 Stammering Aliens 后缀数组&&Hash
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...
- 【JSOI2007】麻将 bzoj 1028
Description 麻 将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西.北.中.发.白七种)和序数牌(分为条子.饼子.万子三种花色,每种花色各有一到 九的九种牌),每种牌各四张 ...
- poj2284 欧拉公式
题意:给出一图形,求该图形把平面分成了几部分 欧拉公式: http://blog.csdn.net/wangxiaojun911/article/details/4586550 对于二维平面上的情况. ...
- UVA11426 欧拉函数
大白书P125 #include <iostream> #include <cstring> using namespace std; #define MMX 4000010 ...
随机推荐
- c# web页面乱码
1.在web.config中加入:<globalization requestEncoding="GB2312" responseEncoding="GB2312& ...
- Bzoj 3831 [Poi2014]Little Bird
3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...
- QT 自定义assert
预览 代码 #define assert_(expression,message) if (expression) \ { \ if (QMessageBox::Yes == QMessageBoxE ...
- Sdut 2409 The Best Seat in ACM Contest(山东省第三届ACM省赛 H 题)(模拟)
题目描述 Cainiao is a university student who loves ACM contest very much. It is a festival for him once ...
- python 面向对象 class 老男孩选课系统
要求:1. 创建北京.上海 2 所学校 class2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. 课程包含,周期,价格,通过学校创建课 ...
- eclipse中DDMS的LOGcat只有一列level
拷贝来源:http://www.cnblogs.com/kobe8/p/4620785.html http://stackoverflow.com/questions/25010393/eclipse ...
- 由 OR 引起的死循环
在客商迁移测试时,程序一旦开始执行就不能自动停止.只能通过手动中断应用服务器的进程来停止.检查迁移的一个表,这个表迁移前没有数据,迁移最多会插入3w条左右数据,但是迁移过程执行2个多小时候再看,已经有 ...
- C++ 11 笔记 (三) : auto
我真的不是标题党... 虽然大一上学期学C语言基础时就学了auto关键字了,而且还是跟static和register两个关键字打包学的,但是.. 猜的没错,C++11这货又给auto加新功能了,在 C ...
- CAGradientLayer的一些属性解析-b
CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...
- C++ QT中的QSound使用方法
在pro文件中添加 QT += multimedia 就可以了