[Gym 101334E]Exploring Pyramids(区间dp)
题意:给定一个先序遍历序列,问符合条件的树的种类数
解题关键:枚举分割点进行dp,若符合条件一定为回文序列,可分治做,采用记忆化搜索的方法。
转移方程:$dp[i][j] = \sum {dp[i + 1][k - 1]*dp[k][j]} $ 令$dp[i][j]$表示i到j里数量
1、记忆化搜索
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9;
ll dp[][];
char a[]; ll dfs(int l,int r){
if(dp[l][r]!=-)return dp[l][r];
if(l==r) return dp[l][r]=;
if(a[l]!=a[r]) return ;
dp[l][r]=;
for(int i=l+;i<=r;i+=){
if(a[i]==a[l]){
dp[l][r]+=dfs(l+,i-)*dfs(i,r);//一定注意dp的顺序
dp[l][r]%=mod;
}
}
return dp[l][r];
} int main(){
freopen("exploring.in", "r", stdin);
freopen("exploring.out", "w", stdout);
while(~scanf("%s", a)){
memset(dp,-,sizeof dp);
int len=strlen(a);
printf("%lld\n", dfs(,len-)%mod);
}
return ;
}
2、区间dp
#include<bits/stdc++.h>
#define mod 1000000000
using namespace std;
typedef long long ll;
string a;
ll dp[][],len;
int main(){
freopen("exploring.in", "r", stdin);
freopen("exploring.out", "w", stdout);
while(cin>>a){
memset(dp,,sizeof dp);
len=a.size();
if(len%==){
cout<<<<endl;
continue;
}
for(int i=;i<len;i++) dp[i][i]=;
for(int i=;i<len;i+=){
for(int l=,r;l+i<len;l++){
r=l+i;
if(a[l]==a[r]){
for(int k=l+;k<=r;k++)
if(a[k]==a[l])
dp[l][r]=(dp[l][r]+dp[l+][k-]*dp[k][r])%mod;
}
}
}
cout<<dp[][len-]<<endl;
}
return ;
}
[Gym 101334E]Exploring Pyramids(区间dp)的更多相关文章
- Gym 101334E Exploring Pyramids(dp+乘法原理)
http://codeforces.com/gym/101334 题意: 给出一棵多叉树,每个结点的任意两个子节点都有左右之分.从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺次记录下来, ...
- UVA 1362 Exploring Pyramids 区间DP
Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...
- 101334E Exploring Pyramids
传送门 题目大意 看样例,懂题意 分析 实际就是个区间dp,我开始居然不会...详见代码(代码用的记忆化搜索) 代码 #include<iostream> #include<cstd ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)
Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...
- Gym - 101196:F Removal Game(区间DP)
题意:一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价. 思路:区间DP即可,dp[i][j]表示[i,j]区间只剩下i和j时的最小代价,那么dp[i][j]=min dp[i ...
- Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)
题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: ...
- LA3516 Exploring Pyramids
Exploring Pyramids 题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树 序列与树构造对应问题,考虑区间DP dp[i][j]表示序列i...j对应 ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
随机推荐
- finally return 执行关系 异常处理 c#
Return.finally执行关系简述 除了函数出现system.exit(0)终止虚拟机,finally中的代码一定执行,return语句会等待finally的执行:如果是值传递,finally中 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- java深入探究04
1.Http协议 tcp/ip关注的是客户端服务器之间数据是否传输成功 http协议:是在tcp/ip协议之间封装的一层协议关注的是传输格式是否规范 注意:系统自带的telent工具(远程访问工具)可 ...
- form 中Enctype=multipart/form-data 的作用
form 中Enctype=multipart/form-data 的作用 ENCTYPE="multipart/form-data"用于表单里有图片上传. <form na ...
- django 更改默认语言和时间
django的默认语言为英语,默认时间为UTC. 在settings.py中更改语言和时间 LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' 改为 LANGUAGE_ ...
- openlayers 3加载GeoServer发布的wfs类型服务
转:https://blog.csdn.net/u013323965/article/details/52449502 问题产生: openlayer3加载WFS存在跨域问题,需要用json ...
- JSP--常用标签
JSTL简介: JSTL是Java中的一个定制标签库集 实现了JSP页面中的代码复用,提高效率 可读性更强,方便前端查看与开发 环境搭建: JSTL标签和Servlet及JSP页面有比较严格的版本对应 ...
- D. String Game 二分加字符串匹配
题目链接 题目大意:给出字符串str1,再第二行给出字符串str2,第三行给出删除str1中的字符的顺序,用数组a[]存,问最多按第三行的顺序删除str1中的字符剩下的字符串中str2 我们定义l为a ...
- codeforces 631B B. Print Check
B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- django学习笔记(三)模型
1.创建一个django app: python manage.py startapp books 2.validate 命令检查你的模型的语法和逻辑是否正确.一旦你觉得你的模型可能有问题,运行 py ...