UVALive 3942 Remember the Word
题意:给出一个由S个不同单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可以重复
使用),有多少种方法?
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
思路:利用字典树上的dp,dp[i]表示从i到末尾有多少种方案,大白书字典树的例题,虽然是例题还是看了一会儿orz,
我太弱了,注意ch数组要开成最大节点数*26的大小,表示每个节点连的字母是什么,初始节点为0,val也要开
成最大节点数的大小,表示该节点是否为单词的最后一个字母。
然后就是dp的过程了,从末尾往前,每次循环从i到结尾,如果碰到某个单词的结尾(也就是val[j]==1)就代表
该单词可以作为某些组合的一部分,dp[i]+=dp[j+1],关键部分就是这里了吧。
代码:
#include<iostream>
#include<string.h>
using namespace std;
const int maxn=4001*101;
const int maxm=3e5+5;
const int mod=20071027;
struct Trie{
int ch[maxn][26];
int val[maxn];
int sz;
Trie(){
sz=1;
memset(ch[0],0,sizeof(ch[0]));
}
int idx(char c){
return c-'a';
}
void insert(char *s,int v){
int u=0,l=strlen(s);
for(int i=0;i<l;i++){
int x=idx(s[i]);
if(ch[u][x]==0){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][x]=sz++;
}
u=ch[u][x];
}
val[u]=v;
}
}e;
char s[maxm];
char a[110];
long long dp[maxm];
int main(){
int t=0;
while(~scanf("%s",s)){
int n;
scanf("%d",&n);
memset(e.ch[0],0,sizeof(e.ch[0]));
e.sz=1;
for(int i=0;i<n;i++){
scanf("%s",a);
e.insert(a,1);
}
int l=strlen(s);
memset(dp,0,sizeof(dp));
dp[l]=1;
int u;
for(int i=l-1;i>=0;i--){
u=0;
for(int j=i;j<l;j++){
int x=e.idx(s[j]);
if(e.ch[u][x]==0)break;
u=e.ch[u][x];
if(e.val[u])dp[i]+=dp[j+1];
}
dp[i]%=mod;
}
//printf("%d\n",e.sz);
//for(int i=0;i<=l;i++)printf("%lld ",dp[i]);
printf("Case %d: %lld\n",++t,dp[0]);
}
return 0;
}
UVALive 3942 Remember the Word的更多相关文章
- 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[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 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- (Trie) uvalive 3942 Remember the word
题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串. 思路:字典树(显然)+DP DP: dp[i]+=dp[j+1] i<=j<=m-1,且i到j的字符串能在字典树中找到.也就 ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
随机推荐
- kafka reset offset 手工重置offset
1.场景 a)有时消费端逻辑修改,需要重复消费数据,需要将offset设置到指定位置. 2.实现 kafka版本:0.11.* KIP-122: Add Reset Consumer Group Of ...
- 非阻塞I/O事件驱动
在 Java.PHP 或者.net 等服务器端语言中,会为每一个客户端连接创建一个新的线程.而每个线程需要耗费大约 2MB 内存.也就是说,理论上,一个 8GB 内存的服务器可以同时连接的最大用户数为 ...
- 洛谷P1039 侦探推理(模拟)
侦探推理 题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情 ...
- yolo v2使用总结
以下都是基于yolo v2版本的,对于现在的v3版本,可以先clone下来,再git checkout回v2版本. 玩了三四个月的yolo后发现数值相当不稳定,yolo只能用来小打小闹了. v2训练的 ...
- source Insight 添加python 工程
1. 下载python的识别文件 Python.CLF 2.设置
- servlet-response学习笔记
为了给用户一个返回数据,我们需要使用HttpServletResponse 从相应对象获取一个输入流 通过输入流将返回结果写入到响应体中 关闭输入流 public class ResponseServ ...
- Go 1.11 Module 介绍
title: "Go 1.11 Module" date: 2018-10-26T23:50:56+08:00 draft: false --- Go 1.11 Module 介绍 ...
- CDNI - RFC 6707 翻译
CDNI的相关问题陈述 概述 CDN对可缓存内容提供了许多好处:降低交付成本,提高终端用户体验,提高交付的鲁棒性.对于这些因素,CDN常 用于大规模的内容交付.因此,现有的CDN提供商正在扩大规模,许 ...
- FTP上传文件,报错java.net.SocketException: Software caused connection abort: recv failed
FTP上传功能,使用之前写的代码,一直上传都没有问题,今天突然报这个错误: java.net.SocketException: Software caused connection abort: re ...
- MPICH2简单的安装配置总结
./configure -prefix=/home/mpi/mpich2 make make install 用命令export PATH /home/mpi/mpich2/bin:$PATH,但我是 ...