UVA 1401 Remember the Word
字典树优化DP
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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>
#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的更多相关文章
- UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...
- UVA 1401 Remember the Word(用Trie加速动态规划)
Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- 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) ...
- uva 1401 dp+Trie
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 1401
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...
- 1401 - Remember the Word
注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...
随机推荐
- Crimm Imageshop 2.3。
下载地址:http://files.cnblogs.com/Imageshop/ImageShop.rar 一款体积小,能绿色执行,又功能丰富的图像处理软件. Imageshop2.3为单EXE文件, ...
- Jmeter实现登录bugfree、新建bug、解决bug脚本(抓包工具实现)
环境 Chrome jmeter3.1 fiddler4 win7 32位 Linux CentOs6.4 bugfree3.0.1 链接:http://pan.baidu.com/s/1gfHpbp ...
- eclipse启动优化,终于不那么卡了!
eclipse启动优化,终于不那么卡了! 网上找了好多都是myEclipse的优化的,跟eclipse有点区别,找了很多方法还是不能让这个eclipse(Version: Kepler Release ...
- grep 命令过滤配置文件中的注释和空行
grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. ...
- font-size 兼容问题
早年~ 楔子 在为“我的抵扣券”添加 按钮时,为了将文字隐掉,给节点设置了“font-size:0;”,设置后刷一下浏览器,webkit下按钮掉下去了,而其他浏览器(包括IE6/7)都正常: 按理说 ...
- 【Git】error: RPC
摘要 git push throws error: RPC failed; result=56, HTTP code = 200的解决办法 原因 默认 Git 设置 http post 的缓存为 ...
- java 导出word 并下载
记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...
- mybatis返回数据类型为map,值为null的key没返回
创建mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- Rabbitmq集群升级方案
升级Rabbitmq 3.6.3版本至3.6.6版本,升级过程中的一些关键步骤记录 Step 1: 顺序关闭集群所有节点,这里注意最后一个关闭的节点必须保证为硬盘节点,而非RAM节点: centOS ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...