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

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的更多相关文章

  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. c语言中继承和多态的简单实现

    C语言本身是不支持继承和多态的,但其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 GObject,它是整个图形界面开发库 GTK 的基石,在IBM developerWor ...

  2. NODE JS拼命吹,我就不喜欢. 别问为什么,直觉.

    NODE JS拼命吹,我就不喜欢. 别问为什么,直觉. 来看看node js 在paypal的捣鼓文章吧.https://www.paypal-engineering.com/2013/11/22/n ...

  3. centOS 6.4 mysql安装

    1 直接安装 yum install mysql mysql-server 2 启动mysql /usr/bin/mysqld_safe --user=mysql & 启动时,如果出现Can' ...

  4. phpstorm使用技巧

    确实很好用,不过还是要看一些方法 转自:http://blog.sina.com.cn/s/blog_488193d70102vk7e.html 2, 关联DOC文档: 右键External Libr ...

  5. Linux网络应用编程之Packet Tracer安装及界面介绍

    Packet Tracer入门 一,Packet Tracer介绍 packet tracer 是由Cisco公司发布的一个辅助学习工具,为学习思科网络课程的初学者去设计.配置.排除网络故障提供了网络 ...

  6. margin系列之内秀篇

    本系列摘自  飘零雾雨的博客 最Cool的利器 一样东西在不同的场景,不同的人手里,所能做的事会有很大不同.我深切的以为 margin 绝对是 CSS 中最有能力的利器之一,不知大家以为然否? 前面几 ...

  7. 入门2:PHP相关的名词解释

    /**宝宝我英语不好,后面注释拼音 请见谅**/ 1.Linux 开源的操作系统,在服务器端用户数量非常大,很多服务器都是使用Linux系统运行的. 相对windows系统来说具有非常完善的用户权限系 ...

  8. 学习PHP爬虫--《Webbots、Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)》

    <Webbots.Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)> 译者序 前言 第一部分 基础概念和技术 第1章 本书主要内容3 1.1 发现互联网的真 ...

  9. 未能解析此远程名称:'nuget.org'

    今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...

  10. c#自动更新+安装程序的制作 (转)

    c#自动更新+安装程序的制作 (转)  http://blog.csdn.net/myhuli120/article/details/6927588 一.自动更新的实现 让客户端实现自动更新,通常做法 ...