UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP;
这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到。
其实就是通过递推,从1扫到最后一位,由d[i]代表1-i位的时候的组合总数,则对d[i]进行扩张,凡是可以从第i+1位到第j位正好对应一个单词,则,d[j]+=d[i];
这样递推完,d[len]即为最后结果
为了表示某个单词的结尾,在字典树中再加一个变量 flag,当单词结尾的时候为1,否则为0,这样只要在树种读到了flag=1的时候,即表示读到这个单词。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
node* ch[];
bool flag;
} tree[];
int cnt,len,d[];
const int MOD=;
node* creat()
{
node*p;
p=&tree[cnt++];
for (int i=; i<; i++)
p->ch[i]=NULL;
p->flag=false;
return p;
}
char s[];
char tmp[];
int ok[];
int num;
void insertn(node* root,char*s1)
{
node* p=root;
int i=;
while (s1[i])
{
int k=s1[i++]-'a';
if (p->ch[k]==NULL)
{
p->ch[k]=creat();
}
p=p->ch[k];
}
p->flag=true;
}
void chk(int add,int jmp,node* root)
{
node* p=root;
for (int i=jmp;i<=len;i++)
{
if (p->ch[s[i]-'a']!=NULL)
{
p=p->ch[s[i]-'a'];
if (p->flag)
{
d[i]=(d[i]+add)%MOD;
ok[i]=;
}
}
else break;
}
}
int main()
{
int kase=;
while (scanf("%s",s+)!=EOF)
{
cnt=;
scanf("%d",&num);
node* root=creat();
for (int i=; i<num; i++)
{
scanf("%s",tmp);
//puts(tmp);
insertn(root,tmp);
}
len=strlen(s+);
//cout<<len<<endl;
memset(d,,sizeof d);
memset(ok,,sizeof ok);
d[]=;
ok[]=;
for (int i=;i<=len;i++)
{
if (ok[i-])
chk(d[i-],i,root);
}
printf("Case %d: %d\n",++kase,d[len]);
}
return ;
}
UVALive 3942 字典树+dp的更多相关文章
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 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 ...
- HDU5715 XOR 游戏 二分+字典树+dp
当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- UVALive 5029 字典树
E - Encoded Barcodes Crawling in process...Crawling failedTime Limit:3000MS Memory Limit:0KB 6 ...
- UVALive 4685 Succession 树DP+背包
一.前言 这道题同样来自于红书P142,作为树DP专题中的一道比较难的题目,A了一天左右的时间,看上去事实证明,这题的难度理我本身的实力还是有些太远了,于是正确的做法应该是分析一下题目之后进行解析什么 ...
随机推荐
- sql语句中 and 与or 的优先级
- Mysql数据库的简单介绍与入门
Mysql数据库的简单介绍与入门 前言 一.下载与安装 1.下载 官网下载MYSQL5.7.21版本,链接地址https://www.mysql.com/downloads/.下载流程图如下: 找到M ...
- 在 CentOS 中部署 KMS 服务器(vlmcsd)
准备 vlmcsd 下载 vlmcsd 本文使用的 vlmcsd 版本为 svn1111,支持的产品: Windows Vista – 10Windows Server 2008 - 2016Offi ...
- oracle 开发注意事项
新建表或字段时,不能使用char,统一使用varcha,防止判断null时有遗漏 新建表,索引,序列,新增删除或修改字段的时候,要先判断操作的对象是否存在,否则SLQ会报错 插入或者修改特殊字符,解决 ...
- cf 763B. Timofey and rectangles
%%题解,脑洞好大啊. 四色定理什么鬼的..所以一定是yes. 因为矩形边长都是奇数,所以可以按左下角分类,一共4类,分别1,2,3,4就可以了. (需要4种颜色的情况大概就是4个矩形围起来一个矩形) ...
- Spring-IOC(基于注解)
1.Spring 的 Bean 管理:(注解方式) 1.1 创建 web 项目,引入 Spring 的开发包: 注:在 Spring 的注解的 AOP 中需要引入 spring-aop 的 jar 包 ...
- 150-PHP nl2br函数(一)
<?php $str="h t m l"; //定义一个多处换行的字串 echo "未处理前的输出形式:<br />{$str}"; #nl2 ...
- RabbitMQ整合Spring Booot【消费者补偿幂等问题】
如果消费者 运行时候 报错了 package com.toov5.msg.SMS; import org.springframework.amqp.rabbit.annotation.RabbitHa ...
- 错误:selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
错误再现 原因:firefox浏览器版本和浏览器驱动版本不匹配 解决办法:卸载高版本浏览器,安装低版本浏览器
- SpringBoot的Banner横幅
SpringBoot的Banner横幅即在SpringBoot应用程序启动过程中,日志输出的如下内容: 如果想替换此部分内容的话,可以在classpath根路径下建立一个文件,命名为:banner.t ...