字典树优化DP

                               Remember the Word
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

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>
#include <string> using namespace std; const int MOD=;
const int maxn=; int m,dp[];
char str[]; struct Trie
{
int tot,root,child[maxn][];
bool flag[maxn];
Trie()
{
memset(child[],,sizeof(child[]));
flag[]=false;
root=tot=;
}
void Init()
{
memset(child[],,sizeof(child[]));
flag[]=false;
root=tot=;
}
void Insert(const char*str)
{
int *cur=&root;
for(const char *p=str;*p;p++)
{
cur=&child[*cur][*p-'a'];
if(*cur==)
{
*cur=++tot;
memset(child[tot],,sizeof(child[tot]));
flag[tot]=false;
}
}
flag[*cur]=true;
}
bool query(const char* str,int i)
{
int *cur=&root;
int l=;
for(const char*p=str;*p&&*cur;p++,l++)
{
cur=&child[*cur][*p-'a'];
if(flag[*cur])
{
dp[i]=(dp[i]+dp[i+l])%MOD;
}
}
return (*cur&&flag[*cur]);
}
}tree; int main()
{
int cas=;
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
scanf("%d",&m);
tree.Init();
while(m--)
{
char dic[];
scanf("%s",dic);
tree.Insert(dic);
}
memset(dp,,sizeof(dp));
dp[len]=;
for(int i=len-;i>=;i--)
{
tree.query(str+i,i);
}
printf("Case %d: %d\n",cas++,dp[]%MOD);
}
return ;
}

UVA 1401 Remember the Word的更多相关文章

  1. UVA 1401 - Remember the Word(Trie+DP)

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...

  2. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  3. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

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

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

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

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

  6. UVa 1401 (Tire树) Remember the Word

    d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...

  7. uva 1401 dp+Trie

    http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. uva 1401

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...

  9. 1401 - Remember the Word

    注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...

随机推荐

  1. 解读ASP.NET 5 & MVC6系列(11):Routing路由

    新版Routing功能介绍 在ASP.NET 5和MVC6中,Routing功能被全部重写了,虽然用法有些类似,但和之前的Routing原理完全不太一样了,该Routing框架不仅可以支持MVC和We ...

  2. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. 红豆电信JS

    http://www.hodo170.com/login.jsp 先定义window,再定义RSAUtils=window.RSAUtils || a.RSAUtils; /* * RSA, a su ...

  4. 【USACO 3.2】Spinning Wheels(同心圆旋转)

    题意: 5个同心圆,告诉你角速度,每个圆有1至5个楔,告诉你起点和宽度.求最早时间如果有的话使得存在某个角度经过5个圆的楔. 题解: 最重要的是要意识到,360秒钟后,每个圆都回到了原来的位置. 我的 ...

  5. Python文本处理nltk基础

    自然语言处理 -->计算机数据 ,计算机可以处理vector,matrix 向量矩阵. NLTK 自然语言处理库,自带语料,词性分析,分类,分词等功能. 简单版的wrapper,比如textbl ...

  6. flex学习小结

    接触到flex一个多月了,今天做一个学习小结.如果有知识错误或者意见不同的地方.欢迎交流指教. 画外音:先说一下,我是怎么接触到flex布局的.对于正在学习的童鞋们,我建议大家没事可以逛逛网站,看看人 ...

  7. Scrum Meeting

    本周Sprint Master 侯宇泰 一. 工作完成内容记录 & 明日计划 · 陈双: 完成内容: 1. 做了一个英语趣配音的用户评价调研 2. 上传了一个新视频 明日计划: 1. 与前端录 ...

  8. 简单的maven配置

    groupId是指com.xx 组织标识 artifactId才是项目名称 2)编译源代码 mvn compile 3)编译测试代码 mvn test-compile 4)清空 mvn clean 5 ...

  9. [bigdata] 从Cloudera Manager中彻底删除服务器

    1. 在CM中主机页里,选定要删除的服务器,选择操作“从群集中删除”. 2. 登录到主机,执行chkconfig,显示有cloudera-scm-agent服务, 执行service cloudera ...

  10. 【Beta】Scrum06

    Info 时间:2016.12.15 21:35 时长:25min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.17 21:30 Task Report Name ...