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 ...
随机推荐
- 工控随笔_13_西门子_WinCC的VBS脚本_04_变量类型之二
上一个随笔说了一些关于vbs变量类型的内容,这一篇我们继续说说变量类型相关的内容. 一.NULL补充内容 '需要注意的是,NULL不能简单通过 = 来进行比较,而必须通过 'IsNull函数来实现 ' ...
- CentOS下puppet安装
简介 Puppet是开源的基于Ruby的系统配置管理工具,puppet是一个C/S结构, 当然,这里的C可以有很多,因此,也可以说是一个星型结构. 所有的puppet客户端同一个服务器端的puppet ...
- 画流程图挺好的软件---visio
visio 2016 下载地址链接: https://pan.baidu.com/s/1oyfJL_QgOA7qUHOt7p8CIA 提取码: mv79 第一次做的样式图:
- 数据库索引的数据结构b+树
b+树的查找过程:如上图所示,如果要查找数据项29,那么首先会把磁盘块1由磁盘加载到内存,此时发生一次IO,在内存中用二分查找确定29在17和35之间,锁定磁盘块1的P2指针, ...
- RobotFramework + Appium 移动自动化实现
本次我们移动自动化实现路线大致是这样的: Python语言基础 —> Robot Framework测试框架 —> Android SDK和Genymotion模拟器 —> ...
- Ajax 请求请求 MVC WebAPI跨域问题;XMLHttpRequest cannot load
问题:XMLHttpRequest cannot load http://192.168.1.4:9010//api/contacts. The 'Access-Control-Allow-Origi ...
- oracle入坑日记<六>自增列创建和清除(含序列和触发器的基础用法)
0 前言 用过 SQLserver 和 MySQL 的自增列(auto_increment),然而 Oracle 在建表设置列时却没有自增列. 查阅资料后发现 Oracle 的自增列需要手动编写. ...
- 容器(docker)内运行Nginx
容器内运行nginx其实很简单,但是一开始还是浪费了我很多时间.这里写下来给大家省点时间. 1.创建nginx文件夹,放置各种配置及日志等. mkdir /docker/nginx docker 文件 ...
- zk开机自动启动脚本
[Unit] Description=Zookeeper service After=network.target [Service] User=www Group=www SyslogIdentif ...
- Picasso, ImageLoader, Fresco, Glide 优劣
Fresco:是Facebook 在今年上半年开源的图片缓存优点:1. 图片存储在安卓系统的匿名共享内存, 而不是虚拟机的堆内存中, 图片的中间缓冲数据也存放在本地堆内存,所以, 应用程序有更多的内存 ...