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

解题关键:枚举分割点进行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. float后怎么居中

    我们在用css float的时候,如何让文本水平居中真的很麻烦, [float]使div(或者其他标签)的宽度自适应其内容,但它却有个弊端:无法居中.[display:inline-block]也有同 ...

  2. 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array

    描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...

  3. 【leetcode刷题笔记】Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  4. poj 1028 Web Navigation 【模拟题】

    题目地址:http://poj.org/problem?id=1028 测试样例: Sample Input VISIT http://acm.ashland.edu/ VISIT http://ac ...

  5. C#实现对外部程序的调用操作

    测试工具,首先也是一个C#的程序,它的主要目的是: 1:获取上文应用程序的窗口句柄,继而获取TextBox句柄及Button句柄: 2:为TextBox随机填入一些字符: 3:模拟点击Button: ...

  6. 《python基础教程(第二版)》学习笔记 字典(第4章)

    <python基础教程(第二版)>学习笔记 字典(第4章)创建字典:d={'key1':'value1','key2':'value2'}lst=[('key1','value1'),(' ...

  7. 1.SVN

    1.SVN安装 SVN服务器端VisualSVN Server(64位OS必须装64位VisualSVN-Server,默认端口是443). 就像建立数据库一样,需要先在svn服务器VisualSVN ...

  8. Linux下查看Python安装了哪些脚本模块

    Linux下查看Python安装了哪些脚本模块 1.什么是rpm ? rpm 即RedHat Package Management,是RedHat的发明之一 .现在包括OpenLinux.fedora ...

  9. linux shell编程(三) if 和 for

    if 条件判断: 单分支的if语句if 判断条件: then statement1fi双分支的if语句if 判断条件;then statement1 statementelse statement3f ...

  10. 使用kill命令终止进程shell脚本

    因有的程序使用kill才能结束掉进程,没有关闭脚本,以我司的服务为例,服务名叫asset-server服务,只有启动脚本,自编写关闭脚本,及重启动脚本. 关闭服务脚本. vim asset-shutd ...