UVALive 3942 Remember the Word(字典树+DP)
题意:一个长字符串和多个短字符串,求短字符串有多少种方式组成长字符串。
状态转移方程: dp[i] = sum(d[i + len(x)]) (x是s[i...L]的前缀)
对于每个i,如果直接暴力寻找s[i...L]的前缀,复杂度为O(nm) (n为短字符串的个数,m为短字符串的长度),肯定会超时。
既然是求前缀,那么可以使用前缀树(字典树)来解决此问题。用字典树寻找前缀的复杂度为O(m)。
#include <cstdio>
#include <iostream>
#include <cstring>
#define maxn 400100
#define maxm 300010
#define mod 20071027
#define sigma_size 26
using namespace std; char str[maxm], tstr[];
int dp[maxm], ch[maxn][sigma_size], val[maxn], sz; struct Trie
{
int get_id(char c)
{
return c - 'a';
} void insert(char* str, int v)
{
int u = , len = (int)strlen(str);
for (int i = ; i < len; ++i)
{
int id = get_id(str[i]);
if (!ch[u][id])
{
memset( ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][id] = sz++;
}
u = ch[u][id];
}
val[u] = v;
} int query(char* str, int start)
{
int u = , result = , len = (int)strlen(str), next;
for (int i = ; i < len; ++i)
{
int id = get_id(str[i]);
next = ch[u][id];
if (next)
{
if (val[next])
result = (result + dp[i + start + ])%mod;
}
else
break;
u = next;
}
return result;
}
}; void init(); int main(void)
{
int ca = , n;
while (scanf("%s", str) != EOF)
{
init();
int len = (int)strlen(str); scanf("%d", &n);
Trie trie = Trie();
while (n--)
{
scanf("%s", tstr);
trie.insert( tstr, );
} dp[len] = ;
for (int i = len - ; i >= ; i--)
{
dp[i] = trie.query( str + i, i);
}
printf("Case %d: %d\n", ca++, dp[]);
}
return ;
} void init()
{
sz = ;
memset( dp, , sizeof(dp));
memset( ch[], , sizeof(ch[]));
memset( val, , sizeof(val));
}
UVALive 3942 Remember the Word(字典树+DP)的更多相关文章
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- LA 3942 - Remember the Word 字典树+DP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
随机推荐
- FMDB最简单的教程-3 清空数据表并将自增字段清零
[db executeUpdate:@"DELETE FROM MemberInfo"]; [db executeUpdate:@"UPDATE sqlite_seque ...
- VS调试技巧
下面有从浅入深的6个问题,您可以尝试回答一下 一个如下的语句for (int i = 0; i < 10; i++){if (i == 5)j = 5;},什么都写在一行,你怎么在j=5前面插入 ...
- 关于对 maximio平台的五个常用类的初步理解及总结
AppBean:绑定应用的默认Bean类,控制主对象集/单个对象,和ui关联的类 继承:DataBean DataBean:任何对象集/单个对象,在ui端处理类. 1.在对象层:写一个Fld类,调用构 ...
- Excel 数值转换为人民币大写金额字符串
把$B$27单元格中的数值转换为人民币大写金额字符串: 目标单元格填入以下公式: =IF($B$27=0,CONCATENATE(IF($B$27<=0,,TEXT(INT($B$27),&qu ...
- hadoop单机and集群模式安装
最近在学习hadoop,第一步当然是亲手装一下hadoop了. 下面记录我hadoop安装的过程: 注意: 1,首先明确hadoop的安装是一个非常简单的过程,装hadoop的主要工作都在配置文件上, ...
- Shape comparison language
形状比较语言, 九交模型 In this topic About shape comparison language Dimensionality Extensions to the CBM SC ...
- Multipart to single part feature
Multipart to single part feature Explode Link: http://edndoc.esri.com/arcobjects/8.3/?URL=/arcobject ...
- 深入理解js——执行上下文
什么是"执行上下文"?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时,已经知道 ...
- Tank游戏需求分析兼项目计划发布!
项目计划 1. 编写目的 此需求文档旨在明确本游戏项目的详细规则和操作方法,供用户理解项目实现的具体功能,并作为项目详细设计开发的基础. 2. 项目背景 市面上游许多魔性小游戏,让广大玩家根本 ...
- anyexec
http://www.codesec.net/view/420386.html http://www.cnblogs.com/qiyebao/p/5362101.html http://www.mon ...