题意:给定一个先序遍历序列,问符合条件的树的种类数

解题关键:枚举分割点进行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)的更多相关文章

  1. Gym 101334E Exploring Pyramids(dp+乘法原理)

    http://codeforces.com/gym/101334 题意: 给出一棵多叉树,每个结点的任意两个子节点都有左右之分.从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺次记录下来, ...

  2. UVA 1362 Exploring Pyramids 区间DP

    Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...

  3. 101334E Exploring Pyramids

    传送门 题目大意 看样例,懂题意 分析 实际就是个区间dp,我开始居然不会...详见代码(代码用的记忆化搜索) 代码 #include<iostream> #include<cstd ...

  4. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  5. LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)

    Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...

  6. Gym - 101196:F Removal Game(区间DP)

    题意:一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价. 思路:区间DP即可,dp[i][j]表示[i,j]区间只剩下i和j时的最小代价,那么dp[i][j]=min  dp[i ...

  7. Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)

    题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: ...

  8. LA3516 Exploring Pyramids

    Exploring Pyramids 题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树 序列与树构造对应问题,考虑区间DP dp[i][j]表示序列i...j对应 ...

  9. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

随机推荐

  1. iOS uitableViewCell 选中 push后返回 取消选中状态

    首先我有一个UITableViewController,其中每个UITableViewCell点击后都会push另一个 ViewController,每次点击Cell的时候,Cell都会被选中,当从p ...

  2. Sql Server2008——存储过程编程简单例子

    主要介绍: 存储过程的定义方法及其使用方法. 实例介绍: 1 创建学生表Student create database Stu use Stu go CREATE TABLE Student ( Sn ...

  3. Java多线程系列 JUC线程池05 线程池原理解析(四)

    转载 http://www.cnblogs.com/skywang12345/p/3544116.html  https://blog.csdn.net/programmer_at/article/d ...

  4. 新手用的git配置命令

    新手用的git配置命令 /**第一次链接远程仓库 本地已有项目需要上传码云 */ //1.配置码云用户名 git config --global user.name "昵称" // ...

  5. EntityFramework 学习 一 Local Data

    DBSet的Local属性提供简单的从context上下文获取当前已经被跟踪的实体(实体不能被标记为Deleted状态) using System.Data.Entity; class Program ...

  6. Java JDK环境配置及说明

    一.Java程序运行机制 Java语言编写的程序需要经过编译生成与平台无关的字节码(.class文件). 这种字节码必须使用Java解释器(JVM)来解释执行. JVM是可运行Java字节码文件的虚拟 ...

  7. Spark- 计算每个学科最受欢迎的老师

    日志类型 测试数据 http://bigdata.myit.com/zhangsan http://bigdata.myit.com/zhangsan http://bigdata.myit.com/ ...

  8. js 跨域复习 window.name | window.domain | iframe | Jsonp

    引起跨域的原因: 浏览器的同源策略,但是当你要发送请求的时候,出于安全性问题,浏览器有严格的要求,必须协议,域名,端口都相同,这个就是同源策略. 影响:a通过js脚本向b发送ajax请求,不同源就会报 ...

  9. window操作常识

  10. Git_错误_01_failed to push some refs to 'git@github.com

    在使用git 对源代码进行push到gitHub时可能会出错,信息如下 此时很多人会尝试下面的命令把当前分支代码上传到master分支上. $ git push -u origin master 但依 ...