题意

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

思路

很像根据中缀和后缀表达式求前缀表达式之类的题。方法自然也差不多。由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. com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver

    com.mysql.jdbc.Driver tocom.mysql.cj.jdbc.Driver MySQL :: MySQL Connector/J 8.0 Developer Guide :: 4 ...

  2. os模块os.walk() 方法和os.path.join()的简单使用

    os.walk:   http://www.runoob.com/python/os-walk.html os.path.join:   https://blog.csdn.net/zmdzbzbhs ...

  3. Python开发【模块】:time、datatime

    时间模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,   ...

  4. android读取通讯录和使用系统通讯录

    第一步:注册权限 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <us ...

  5. JS中手动触发事件的方法

    如果大家将一张网页看成一个form的话,大致上就成了一个web form的模型.在win form 下要想手动触发某一个对象的事件是很简单的,只要发送一条消息即可达成.(PostMessage) 但是 ...

  6. Mac/OSX上安装xshell

    xshell没有mac版,且不愿意仅为一个程序运行一个虚拟机.怎么办?装上wine个来跑shell吧! 1.安装 WineBottler 过程略(制作.管理windows程序,类似CrossOver) ...

  7. 多线程Java面试题总结

    57.Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别?答:sleep()方法(休眠)是线程类(Thread)的静态方法,调用此方法会让当前线程暂停执行指 ...

  8. Linux系统——Inotify事件监控工具

    每秒传输文件200个 Rsync放在定时任务中也只是一分钟执行一回,要想达到实时的效果,为防止单点nfs架构故障,再启动一台nfs服务器作为主nfs服务器的备份服务器,此时需要inotify实时同步数 ...

  9. cocos代码研究(24)Widget子类PageView学习笔记

    理论基础 PageView类又称Layout的管理器,可以让用户在多个Layout之间左右或者上下切换显示,继承自 Layout . 代码实践 static PageView * create ()创 ...

  10. 解决android studio 模拟器取法启动声音的错误

    Emulator: dsound: Reason: No sound driver is available https://jingyan.baidu.com/article/a65957f4348 ...