题意

输入一棵树的后缀表达式(按左-右-中顺序访问),这棵树的每一个结点的数值都比它的左子树结点的数值大,而比它的右子树结点的数值小,要求输出其按右-左-中顺序访问的表达式。所有的数都为正整数,而且不会重复。

思路

很像根据中缀和后缀表达式求前缀表达式之类的题。方法自然也差不多。由DFS的括号性质可知,每一个树都对应表达式的一个区间,而此题中区间的最后一个就是树的根,然后根据根的大小可以把区间分为值小于和大于根值的两部分,即左子树和右子树,然后递归地输出右子树、左子树,最后再输出根即可。

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;

const int maxn = 3005;
int a[maxn];
void dfs(int l, int r){
if (l > r) return ;
if (l == r){
printf("%d ", a[r]);
return ;
}
int x = a[r];
int p = r - 1;
while(p >= l && a[p] > x) p --;
dfs(p+1, r-1);
dfs(l, p);
printf("%d ", a[r]);
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++){
scanf("%d", &a[i]);
}
dfs(0, n-1);
return 0;
}
[/cpp]

URAL 1136 Parliament (DFS)的更多相关文章

  1. ural 1136. Parliament

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1136 题目描述:给定一个按照(左子树-右子树-根)(即先序)遍历序列的树,求其按照 右子 ...

  2. URAL 1136 Parliament 二叉树水题 BST后序遍历建树

    二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...

  3. URAL.1033 Labyrinth (DFS)

    URAL.1033 Labyrinth (DFS) 题意分析 WA了好几发,其实是个简单地DFS.意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了. 代码总览 #include <io ...

  4. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  5. timus 1136 Parliament(e)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  6. 1136. Parliament(二叉树)

    1136 先由后左 再父 建一个二叉树 #include <iostream> #include<cstdio> #include<cstring> #includ ...

  7. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  8. URAL 1208 Legendary Teams Contest(DFS)

    Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...

  9. URAL 1137Bus Routes (dfs)

    Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

随机推荐

  1. webView返回不刷新

    [[NSUserDefaults standardUserDefaults] setInteger:2 forKey: @"WebKitCacheModelPreferenceKey&quo ...

  2. 最近遇到的bug

    1. 地图周边快查,按钮点击没反应 子控件超出了父控件 2.图片显示灰色背景,一直去不掉    设置图片背景图片clear cloro  3. 显示隐藏导航栏  下面两个方法效果不同     self ...

  3. 我希望知道的关于Django的11件事(转)

    英文原文:https://medium.com/cs-math/f29f6080c131 译文:http://my.oschina.net/chenlei123/blog/270672 两年前, 我开 ...

  4. centos Linux系统日常管理1 cpuinfo cpu核数 命令 w, vmstat, uptime ,top ,kill ,ps ,free,netstat ,sar, ulimit ,lsof ,pidof 第十四节课

    centos Linux系统日常管理1  cpuinfo cpu核数   命令 w, vmstat, uptime ,top ,kill ,ps ,free,netstat ,sar, ulimit ...

  5. (转)在 ListViewItem 上拖动进行框选

    public partial class Form1 : Form { private bool IsMouseDown = false; Rectangle MouseRect = Rectangl ...

  6. 11. Container With Most Water(装最多的水 双指针)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. 2017 ACM/ICPC Asia Regional Qingdao Online Solution

    A : Apple 题意:给出三个点,以及另一个点,求最后一个点是否在三个点的外接圆里面,如果在或者在边界上,输出“Rejected”,否则输出"Accepted" 思路:先求一个 ...

  8. 高可用Mysql架构_Haproxy+keepalived+mycat集群的配置

    接上一篇文章http://www.cnblogs.com/wt645631686/p/8310891.html,虽然已经在原有两台mycat基础上增加了Haproxy作为mycat的中间服务器,不足的 ...

  9. Java生成PDF之iTextPDF的使用

    今天做财务方面相关数据的导出功能,需要导出PDF和Excel,在项目经理那里得知有一个叫iTextPDF的java框架导出PDF文件很好用,于是拿来玩儿玩儿. package com.smart.pr ...

  10. Python3.x:自动生成IP写入文本

    Python3.x:自动生成IP写入文本 ''' 生成ip写入文件 ''' import time time_start = time.time() #参数:number-生成条数:start-开始i ...