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

解题关键:枚举分割点进行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. python学习笔记20160413

    1. type(val) #查看val的类型. 2. 出现错误的时候, 读懂错误信息.3. raw_input('xxx') #读取用户输入都是string类型数据.4. ValueError: in ...

  2. nginx rewrite标签配置以及用户认证配置

    一.nginx  rewrite标签 rewrite 实现URL的改写主要是实现伪静态 1.  rewrite指令语法 指令语法:rewrite regex replacement[flag] 默认值 ...

  3. Apache 的 httpd.conf 注释

    ServerRoot “/usr/local“ ServerRoot用于指定守护进程httpd的运行目录,httpd在启动之后将自动将进程的当前目录改变为这个目录,因此如果设置文件中指定的文件或目录是 ...

  4. android6.0 外部存储设备插拔广播以及获取路径(U盘)【转】

    本文转载自:https://blog.csdn.net/zhouchengxi/article/details/53982222 这里我将U盘作为例子来说明解析. android4.1版本时U盘插拔时 ...

  5. [算法]数组的partition调整

    题目一: 给定一个有序数组arr,调整arr使得这个数组的左半部分没有重复部分且升序,而不用保证右部分是否有序. 例如:arr=[1,2,2,2,3,3,4,5,6,6,7,7,8,8,9,9],调整 ...

  6. [算法]旋转矩阵问题(Spiral Matrix)

    题目一: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

  7. hive启动时 Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected

    错误提示信息如下 [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleC ...

  8. MySQL- 常用的MySQL函数,指令等

    MySQL查看版本: status: 或者 select version(); //select @@version MySQL昨天, 一周前 ,一月前 ,一年前的数据 这里主要用到了 DATE_SU ...

  9. C++(三)— 二维容器

    1.二维bool向量 vector<vector<bool>> dp(len, vector<bool>(len, false));

  10. JS字符串类型转日期然后进行日期比较

    1.字符串转日期格式 var stringToDate = function(dateStr,separator){ if(!separator){ separator="-"; ...