大白书 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> , 1S
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 ...
随机推荐
- 初尝 Perl
本文将阐述以下几方面内容: 1.什么是Perl 2.Perl有什么用 3.Windows 下的Perl环境搭建 4.Perl 版Hello World 5.Perl 语法梗概 6.一些参考资料 什么是 ...
- java新手笔记17 参数
package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, ...
- spring mvc 笔记
springmvc 课堂笔记 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, ...
- UVA 11464 Even Parity(部分枚举 递推)
Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...
- 数位DP入门Ural1057
CF一战让我觉得很疲倦,所以今天感觉很慢. 昨天解D题时候,因为太累,根本连题目都没看,今天看了之后感觉不会做,听闻是数位DP问题. 有某神说过,DP的功力建立在刷过的题上,我真的毫无功力可言. 介绍 ...
- (转)linux多线程,线程的分离与结合
转自:http://www.cnblogs.com/mydomain/archive/2011/08/14/2138454.htm 线程的分离与结合 在任何一个时间点上,线程是可结合的(joi ...
- MinGW 仿 linux 开发环境
MinGW 默认安装 MSYS.通常打开的 MinGW Shell 其实 MSYS,MinGW 作为一个组件存在. MSYS -- Minimal SYStem,是一个 Bourne Shell 解释 ...
- 身份证js验证
<script type="text/javascript"> //--身份证号码验证-支持新的带x身份证 function isIdCardNo(num) { var ...
- 以查询方式实现1s定时
以查询控制器的控制位状态来实现1s定时. #include <reg52.h> sbit LED = P0^; unsigned ; void main () { LED = ; // 点 ...
- Java 单链表的倒置
在面试,笔试的过程中经常会遇到面试官问这种问题,实现单链表的倒置方法.现在对单链表的倒置犯法做个记录,方便自己以后查看. 单链表的定义: public class Node { int v; Node ...