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了一天左右的时间,看上去事实证明,这题的难度理我本身的实力还是有些太远了,于是正确的做法应该是分析一下题目之后进行解析什么 ...
随机推荐
- .net 4.0 : Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.****'
解决办法,添加 MicroSoft.CSharp 的引用.
- ORACLE添加新用户并配置权限 添加其他用户的表权限
添加用户配置权限 1.查出表空间所在位置 ,file_name from dba_data_files order by file_id; 2.根据步骤1查出的路径.将路径替换掉并指定用户名 路径:D ...
- 七、Vue组件库:Element、Swiper(轮播专用组件)
一.vue的Element组件库 官网:https://element.eleme.cn/#/zh-CN 1.1安装 推荐安装方法: 首先要进入项目目录 cnpm i element-ui -S 或 ...
- 从0开始自己配置一个vps虚拟服务器(2)
配置php环境 1.安装php安装所依赖的包 yum -y install gcc gcc-c++ libxml2 libxml2-devel 2.cd usr/local/src 进入目录,在这个目 ...
- 解决Elasticsearch索引只读
今天添加索引时发现kibana添加索引不生效,页面也没有报错,没有创建成功只是一闪而过. 另外发现各项目日志与当前时间差异很大,filebeat一直报错io timeout 具体报错如下: fileb ...
- POJ 1458:Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41957 Accepted: 16 ...
- Windows按键消息—虚拟键码(转)
源地址:http://c.biancheng.net/cpp/html/1253.html 虚拟键码保存在WM_KEYDOWN.WM_KEYUP.WM_SYSKEYDOWN和WM_SYSKEYUP消息 ...
- Exchange Server 2010邮件策略与遵从性
本文档附带了一个附档,里面详细的描述了如何在Exchange Server 2010中配置如下内容: 1.邮件分类 2.免责申明 3.配置信息隔离墙 4.邮件审核 5.Exchange 2010与AD ...
- R 读取excel的方法
1.加载 readxl 包,利用 reade_excel() 函数 install.packages("readxl") library(readxl) data = read_e ...
- Git提交出现error: src refspec master does not match any的问题
在本地与远程分别新建了一个仓库并且关联后,想要把本地的文件提交到github上面, 输入下方命令后没有成功,而是出现了报错:error: src refspec master does not mat ...