Exploring Pyramids

题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树

序列与树构造对应问题,考虑区间DP

dp[i][j]表示序列i...j对应二叉树个数

初始i == j,dp[i][j] = 1

dp[i][j] = 0,i!=j

转移:dp[i][j] = sum(dp[i + 1][k - 1] * dp[k][j]),s[i] == s[j]  即考虑从i出发,在k这个位置回来,然后再从k出发,到j的时候回来

被MOD卡了一下

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(long long &a, long long &b)
{
long long tmp = a;a = b;b = tmp;
}
inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
} const long long INF = 0x3f3f3f3f;
const long long MAXN = + ;
const long long MOD = ; long long dp[MAXN][MAXN],n;
char s[MAXN]; int main()
{
while(scanf("%s", s + ) != EOF)
{
n = strlen(s + );
memset(dp, , sizeof(dp));
for(register long long i = ;i <= n;++ i) dp[i][i] = ;
for(register long long k = ;k <= n;++ k)
for(register long long i = ;i <= n;++ i)
{
long long j = i + k - ;
if(j > n) break;
if(s[i] != s[j]) continue;
for(register long long k = i + ;k <= j;++ k)
if(s[i] == s[k]) dp[i][j] += dp[i + ][k - ] * dp[k][j] % MOD, dp[i][j] = dp[i][j] >= MOD ? dp[i][j] - MOD : dp[i][j];
}
printf("%lld\n", dp[][n]);
}
return ;
}

LA3516

LA3516 Exploring Pyramids的更多相关文章

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

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

  2. UVA 1362 Exploring Pyramids 区间DP

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

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

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

  4. 101334E Exploring Pyramids

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

  5. [Gym 101334E]Exploring Pyramids(区间dp)

    题意:给定一个先序遍历序列,问符合条件的树的种类数 解题关键:枚举分割点进行dp,若符合条件一定为回文序列,可分治做,采用记忆化搜索的方法. 转移方程:$dp[i][j] = \sum {dp[i + ...

  6. LA 3516 - Exploring Pyramids

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. LA 3516 Exploring Pyramids (记忆化搜索)

    题意 给定一个DFS序列,问能有多少树与之对应. 思路 设输入序列为S,dp(i, j)为子序列Si, Si+1, --, Sj对应的树的个数,则边界条件为d(i, i) = 1,且Si != Sj时 ...

  8. LA 3516 (计数 DP) Exploring Pyramids

    设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ...

  9. Exploring Pyramids UVALive - 3516 (记忆化DP)

    题意:给定一个序列 问有多少棵树与之对应 题目连接:https://cn.vjudge.net/problem/UVALive-3516 对于这一序列  分两种2情况 当前分支 和 其它分支  用df ...

随机推荐

  1. 09_springmvc图片上传

    一.上传图片 1.需求 在修改商品页面,添加上传商品图片的功能 2.springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data&quo ...

  2. 石子合并问题 /// 区间DP oj2025

    Description 在一个圆形操场的四周摆放着n堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的两堆石子合并成新的一堆,并将新得的这堆石子数记为该次合并的得分. 试设计一个算法,计算出 ...

  3. scull 中的设备注册

    在内部, scull 使用一个 struct scull_dev 类型的结构表示每个设备. 这个结构定义为: struct scull_dev { struct scull_qset *data;  ...

  4. css3 ---2 属性的选择器

    存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...

  5. ActiveMQ 知识点

    消息队列高可用 持久化,事务,签收,zookeeper+replicated-leveldb-store的主从集群 异步发送 同步发送: 明确指定同步发送 未使用事务的前提下,发送持久化消息(会使用同 ...

  6. Python3基础笔记_字符串类型

    # 1.Python转义字符 a = "sqwerdf" # 2.Python字符串运算符 ''' + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符 ...

  7. 阿里云MaxCompute 2019-8月刊

    您好,MaxCompute 2019.8月刊为您带来8月产品.技术最新动态,欢迎阅读. 导读 [重要发布]8月产品重要发布 [文档更新]8月重要文档更新推荐 [干货精选]8月精选技术文章推荐 [精彩活 ...

  8. Joomla - 后台系统(功能简介)

    Joomla - 后台系统简介 全局配置

  9. Laravel 不同环境加载不同的.env文件

    假设有4个.env文件.分别为 .env.local .env.dev .env.test .env.prd 方式一 第一步:bootstrap\app.php 加入代码 $envs = ['loca ...

  10. light oj 1422 区间dp

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...