[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. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
随机推荐
- 编写你的第一个django应用程序4
本教程上接教程3,我们将继续开发网页投票应用,本部分将主要关注简单的表单处理以及如何对代码进行优化 写一个简单的表单 让我们更新一下在上一个教程中编写的投票详细页面的模板(‘polls/detail. ...
- php正则表达式和数组
一.正则表达式 1. “/”代表定界符,"^"代表起始符号,"$"代表结束符号 $str1="abc123def45ghjk6789lou" ...
- INT(M)表示什么意思?
根据官方文档描述,int(M)中的M表示数据显示的宽度,与实际存储的长度无关. 1.也就是int(3)和int(11)能够存储的数据是一样的,都是从-2147483648到2147483647(或者0 ...
- [算法]数组的partition调整
题目一: 给定一个有序数组arr,调整arr使得这个数组的左半部分没有重复部分且升序,而不用保证右部分是否有序. 例如:arr=[1,2,2,2,3,3,4,5,6,6,7,7,8,8,9,9],调整 ...
- 查找mysql的cnf文件位置
mysql --help|grep 'my.cnf' 查看mysql启动时读取配置文件的默认目录 命令 mysql --help|grep 'my.cnf' 输出 order of preferenc ...
- EntityFramework 学习 一 Add Entity Graph using DbContext:
//Create student in disconnected mode Student newStudent = new Student() { StudentName = "New S ...
- Shiro身份认证-JdbcRealm
Subject 认证主体 Subject认证主体包含两个信息 Principals : 身份,可以是用户名.邮箱.手机号等,用来标识一个登录主体身份. Credentials : 凭证,常见有密码,数 ...
- Tkenter之API测试系统界面设计
# -*- coding: UTF-8 -*- from Tkinter import * tk=Tk() tk.geometry('500x400+500+200') tk.title('API测试 ...
- Windows下控制Nginx的状态
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...
- Javascript- Javascript学习
Javasrcipt的引入方式 内部引入方式 直接将javascript代码写入到<script type="text/javascript"></script& ...