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 ...
随机推荐
- Centos6两个镜像文件的合并方法
1.相关目录: /mnt/dvd1和/mnt/dvd2 用于挂载 Centos 镜像 /mnt/dvd3 合并后的镜像文件 /mnt/iso ISO储存 mkdir -p /mnt/dvd1 /mnt ...
- 浅谈SPI总线
SPI总线概述 SPI全称是串行外设接口(Serial Peripheral Interface),是由Motorola提出的一种全双工同步串行通信接口,通信波特率可以高达5Mbps,但具体速 ...
- JavaScript 原型和对象创建底层原理
1. prototype/__proto__/constructor JS原型链和继承网上已经烂大街了,5毛可以买一堆,这里只提一下: constructor:普通对象和函数对象都有,指向创建它的函数 ...
- 服务器变更IP地址后SSH链接失败的解决办法
客户端未变,服务器端变更IP地址,导致客户端链接失败,这种情况提示如下: 原因是服务器端更改IP地址后,秘钥也需更新 在客户端输入以下格式的命令: ssh-keygen-f"/home/用户 ...
- python-setuptool安装
安装setuptools时报error: ”RuntimeError: Compression requires the (missing) zlib module“ 解决办法: yum安装zlib和 ...
- 20175236 2018-2019-2 《Java程序设计》第五周学习总结
教材学习内容总结 接口回调 1.接口属于引用型变量,可以存放实现该接口类的实例的引用,即存放对象的引用. 2.接口回调理解上跟对象的上转型对象差不多. 理解接口 接口可以抽象出重要的行为标准. 接口多 ...
- gevent-websocket初识
初试 from flask import Flask, request from geventwebsocket.handler import WebSocketHandler from gevent ...
- 学习MeteoInfo二次开发教程(九)
最终的MaskOut功能未能实现 另外,一个有用的,在指定位置显示图片: legend.MarkerType = MarkerType.Image; legend.ImagePath = " ...
- C# Winform开发程序调用VLC播放器控件播放视频.
VLC是个好东西,支持的格式多,还无广告,关键还有调用它的播放控件不用安装. 开个文章记录下调用这个控件的流水账,以便以后需要的时候查阅 创建工程 首先新建一个Winform工程. 这里姑且叫做VLC ...
- hive动态分区问题--分区为中文
报错如下: Loading data to table data_da.tmp_wlw_test partition (stat_date=2017-05-11, business_type_name ...