Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowingthat 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’sonly 20071027 sticks, he can only record the remainders of the numbers divided by total amount ofsticks.The problem is as follows: a word needs to be divided into small pieces in such a way that eachpiece is from some given set of words. Given a word and the set of words, Jiejie should calculate thenumber 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 wordwhose 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 100characters 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.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

这是一道很好的题目,(竟然是用到trie和dp~~).先把单词做成trie(字典树的形式),然后用动态规划,用dp[i]表示文本串第i个字符后面(既s[i....n])所形成的做多数量,然后推出转移方程dp[i]=dp[i]+dp[i+len[j]];

代码如下:

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;

const int maxnode = 4000 * 100 + 10;
const int sigma_size = 26;
const int maxl = 300000 + 10; 
const int maxw = 4000 + 10;   
const int maxwl = 100 + 10;   
const int MOD = 20071027;

int d[maxl], len[maxw], S;
char text[maxl], word[maxwl];

struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz; 
void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } 
int idx(char c) { return c - 'a'; } 


void insert(const char *s, int v) 
{
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++) 
{
int c = idx(s[i]);
if (!ch[u][c]) 

memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;  
ch[u][c] = sz++; 
}
u = ch[u][c]; 
}
val[u] = v; 
}


void find(const char *s, int len, vector<int>& ans) {
int u = 0;
for (int i = 0; i < len; i++) 
{
int c = idx(s[i]);
if (!ch[u][c]) break;
u = ch[u][c];
if (val[u]) ans.push_back(val[u]); 
}
}
} trie;

int main() 
{
int kase = 1;
while (~scanf("%s%d",text,&S)) 
{
trie.clear();
for (int i = 1; i <= S; i++) 
{
scanf("%s", word);
len[i] = strlen(word);
trie.insert(word, i);
}
memset(d, 0, sizeof(d));
int L = strlen(text);
d[L] = 1;
for (int i = L - 1; i >= 0; i--) 
{
vector<int> p;
trie.find(text + i, L - i, p);
for (int j = 0; j < p.size(); j++)
d[i] = (d[i] + d[i + len[p[j]]]) % MOD;
}
printf("Case %d: %d\n", kase++, d[0]);
}
return 0;
}

Remember the Word (UVA-1402)的更多相关文章

  1. PHP:导出数据到word(包含图片)

    1.方法 public function word() { $xlsModel = M('api_aliucheng'); $Data = $xlsModel->Field('id,u_name ...

  2. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

  3. Excel VBA 操作 Word(入门篇)

    原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人.   一.新建Word引用 需要首先创建一个对 Word A ...

  4. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. 并查集(UVA 1106)

    POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...

  6. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  7. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  8. 把数据输出到Word (组件形式)

    上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ...

  9. java导出2007版word(docx格式)freemarker + xml 实现

    http://blog.csdn.net/yigehui12/article/details/52840121 Freemarker+xml生成docx 原理概述:word从2003版就支持xml格式 ...

  10. 55.storm 之 hello word(本地模式)

    strom hello word 概述 然后卡一下代码怎么实现的: 编写数据源类:Spout.可以使用两种方式: 继承BaseRichSpout类 实现IRichSpout接口 主要需要实现或重写几个 ...

随机推荐

  1. 在控制器中如何对frxml的控件初始化

    如果在控制器中实现Initializable这个接口,并重iInitializable这个方法 对于一个fxml文件来说它首先执行控制器的构造函数,这个时候它是无法对@FXML修饰的方法进行访问的,然 ...

  2. 基于docker实现redis高可用集群

    基于docker实现redis高可用集群 yls 2019-9-20 简介 基于docker和docker-compose 使用redis集群和sentinel集群,达到redis高可用,为缓存做铺垫 ...

  3. 过滤条件的时候用between和<>的区别

    无论是sqlsever还是oracle都支持between函数, 2个函数的基本语法是 WHERE A BETWEEN 1 AND 2/ WHERE A >=1 AND A <=2 ,be ...

  4. Head First设计模式——命令模式

    前言:命令模式我们平常可能会经常使用,如果我们不了解命令模式的结构和定义那么在使用的时候也不会将它对号入座. 举个例子:在winform开发的时候我们常常要用同一个界面来进行文件的下载,但是并不是所有 ...

  5. 05-商品类别数据和VUE展示

    一.商品类别数据和VUE展示 1.商品类别数据接口 将商品类别数据展示出来,视图(views.py)代码如下: class CategoryViewset(mixins.ListModelMixin, ...

  6. nyoj 169-素数 (打表)

    169-素数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:42 提交数:84 难度:1 题目描述: 走进世博园某信息通信馆,参观者将获得前所未有的尖端互动体验,一场充满创想和喜悦 ...

  7. 20191107-3 beta week 2/2 Scrum立会报告+燃尽图 02

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9955] 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 ...

  8. 扛把子组2018092609-2 选题 Scrum立会报告+燃尽图 06

    此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8681] 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名 ...

  9. scrapy抓取豆瓣电影相关数据

    1. 任务分析及说明 目标网站:https://movie.douban.com/tag/#/ 抓取豆瓣电影上,中国大陆地区,相关电影数据约1000条:数据包括:电影名称.导演.主演.评分.电影类型. ...

  10. centos7环境搭建一台mysql服务器启动多个端口

    在一台服务器上启动多个mysql实例,分别用不同的端口号,因centos7版本安装mysql5.7后不存在mysqld_multi .mysqld_safe等命令,做踩坑总结 Mysql多实例实现的3 ...