UVALive - 3942 Remember the Word (Trie + DP)
题意:
给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章。
思路:
用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i]转移过来, 注意dp[L+1]要初始化为1.
递推写法
#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + ;
const int mod = ;
char in[maxN];
int n;
struct Trie {
int ch[maxN][];
int val[maxN];
int sz;
void Init(){sz = ; memset(ch[], , sizeof(ch[])); memset(val, , sizeof(val));}
int idx(char c) {return c - 'a';}
void Insert(char *s){
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] = n;
} }tree;
long long dp[maxN];
int main(){
// freopen("1.txt","r", stdin);
int kase = ;
while(~scanf("%s", in)){
tree.Init();
memset(dp , , sizeof(dp));
scanf("%d", &n);
char word[];
for(int i = ; i < n; i++){
scanf("%s", word);
tree.Insert(word);
}
int len = strlen(in);
dp[len] = ;
for(int pos = len ; pos >= ; pos--){
int u = ;
for(int i = pos, wordLen = ; i < len; i++, wordLen++){
int c = tree.idx(in[i]);
if(tree.ch[u][c] == ) break;
u = tree.ch[u][c];
if(tree.val[u] != ){
dp[pos] += dp[pos + wordLen];
dp[pos] %= mod;
}
}
}
printf("Case %d: %lld\n", kase++, dp[]);
} return ;
}
记忆化搜索
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e6 + ;
const int mod = ;
char in[maxN];
int n;
struct Trie {
int ch[maxN][];
int val[maxN];
int sz;
void Init(){
sz = ;
memset(ch[], , sizeof(ch[]));
memset(val, , sizeof(val));
}
int idx(char c) {return c - 'a';}
void Insert(char *s){
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] = n;
} }tree; long long dp[maxN];
int dfs(int pos){
int temp = pos;
if(dp[pos] > ) return dp[pos];
int ret = ;
int adr = ;
while(tree.ch[adr][in[pos] - 'a']){
adr = tree.ch[adr][in[pos] - 'a'];
if(tree.val[adr] != ){
ret = (ret + dfs(pos + )) % mod;
}
pos ++; }
dp[temp] = ret;
return ret;
} int main(){
freopen("1.txt","r", stdin);
int ncase = ;
while(~scanf("%s", in)){
tree.Init();
scanf("%d", &n);
char word[];
for(int i = ; i < n; i++){
scanf("%s", word);
tree.Insert(word);
} int l = strlen(in);
for(int i = ;i <= l; i ++)dp[i] = ;
dp[l] = ;
printf("Case %d: %d\n",ncase ++ , dfs());
} return ;
}
UVALive - 3942 Remember the Word (Trie + DP)的更多相关文章
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- UvaLive3942(Trie + dp)
查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...
- 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 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
随机推荐
- SSIS-Dtsx包文件打开时一直验证
把每个项的DelayValidation设置为true. 也可以直接改文件: 把文件里的 <DTS:Property DTS:Name="DelayValidation"&g ...
- MySQL 查看表大小
当遇到数据库占用空间很大的情况下,可以用以下语句查找大数据量的表 SELECT TABLE_NAME ,),) 'DATA_SIZE(M)' ,),) 'INDEX_SIZE(M)' ,AVG_ROW ...
- 有关在python中使用Redis(一)
python作为一种处理数据的脚本语言本身有许多方法函数供大家使用,有时候为了提升数据处理速度(如海量数据的访问或者海量数据的读取),涉及分布式管理架构,可能需要用到Redis,Redis是一个开源的 ...
- 数据库(数据库、表及表数据、SQL语句)
数据库MYSQL 今日内容介绍 u MySQL数据库 u SQL语句 第1章 数据库 1.1 数据库概述 l 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储 ...
- ABAP扫雷游戏
. INCLUDE <icon>. CONSTANTS: " >> board cell values blank_hidden ', blank_marked TY ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- Android Doze模式源码分析
科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...
- 数据库limit子句
limit子句:用来限定语句执行结果的偏移量,有一个或者两个参数:第一个参数表示返回结果首行偏移量,第二个参数表示最大返回行数.例如:SELECT * FROM employees ORDER BY ...
- optparse 模块
一.optparse是专门用来在命令行添加选项的一个模块.支持python2.3及以上版本,从2.7版本之后,python不再更新该模块,2.7之后的版本推荐使用argparse模块. 二.optpa ...
- jsp另外五大内置对象之response-操作重定向
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...