UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)

芯片缺陷检测全流程:从光学成像到AI自动判别的实战路线 在半导体制造中,良率是衡量产线效率的核心指标,而缺陷检测则是守住良率的关键防线。传统人工显微镜目检已难以应对微米级乃至纳米级缺陷的挑战,工业界正加速转向机器视觉与深度学习相结合的技术方案。其核心原理在于:先通过明场、暗场或DIC等光学成像获取高对比度图像,再借助传统图像处理算法快速提取候选缺陷区域,最后利用CNN等深度学习模型对缺陷进行精准分类与判定。这一技术路线不仅大幅提升检测效率与准确性,还能有效降低漏检和误报,广泛应用于晶圆制造、封装测试等环节。本文从成像选型、算法分工、数据标注到模型部署,系统梳理了 阅读详情

11234 - Expressions

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2175

Arithmetic expressions are usually written with the operators in between the two operands (which is called infix notation). For example, (x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write a program to evaluate an expression if the expression is written in postfix notation (also known as reverse polish notation). In postfix notation, an operator is written behind its two operands, which may be expressions themselves. For example, x y + z w - * is a postfix notation of the arithmetic expression given above. Note that in this case parentheses are not required.

To evaluate an expression written in postfix notation, an algorithm operating on a stack can be used. A stack is a data structure which supports two operations:

  1. push: a number is inserted at the top of the stack.
  2. pop: the number from the top of the stack is taken out.

During the evaluation, we process the expression from left to right. If we encounter a number, we push it onto the stack. If we encounter an operator, we pop the first two numbers from the stack, apply the operator on them, and push the result back onto the stack. More specifically, the following pseudocode shows how to handle the case when we encounter an operator O:

a := pop();
b := pop();
push(b O a);

The result of the expression will be left as the only number on the stack.

Now imagine that we use a queue instead of the stack. A queue also has a push and pop operation, but their meaning is different:

  1. push: a number is inserted at the end of the queue.
  2. pop: the number from the front of the queue is taken out of the queue.

Can you rewrite the given expression such that the result of the algorithm using the queue is the same as the result of the original expression evaluated using the algorithm with the stack?

Input Specification

The first line of the input contains a number T (T ≤ 200). The following T lines each contain one expression in postfix notation. Arithmetic operators are represented by uppercase letters, numbers are represented by lowercase letters. You may assume that the length of each expression is less than 10000 characters.

Output Specification

For each given expression, print the expression with the equivalent result when using the algorithm with the queue instead of the stack. To make the solution unique, you are not allowed to assume that the operators are associative or commutative.

Sample Input
2
xyPzwIM
abcABdefgCDEF
Sample Output
wzyxIPM
gfCecbDdAaEBF

思路:

1. 怎么建树?——对于给出的序列, 从左到右遍历,遇到代表数字的小写则建立一个无儿子的树(只保存值),然后把结点指针入栈;遇到代表操作符的大写字母,则从栈中弹出两个结点,然后建立一个以大写字母为根,弹出的两个节点为左右儿子的树,再把这个新树的根结点指针压入栈。如此循环下去。 最后,在栈顶的那个指针就是最后建成的树的根结点。 

2. 怎么遍历输出?——模拟queue进行bfs扫描整棵树,扫描完成后逆序输出即可。


完整代码:

/*0.049s*/

#include<cstdio>
#include<cstring>
#include<cctype>
const int maxn = 10005;

struct Node
{
	char ch;
	struct Node* left;
	struct Node* right;
} node[maxn];

Node* stack[maxn];
Node* ans[maxn];
char s[maxn];

void bfs(Node* root)
{
	ans[0] = root;
	int begin = 0, end = 1;
	while (begin < end)
	{
		if (ans[begin]->left) ans[end++] = ans[begin]->left;
		if (ans[begin]->right) ans[end++] = ans[begin]->right;
		++begin;
	}
	for (int i = end - 1; i >= 0; --i)
		putchar(ans[i]->ch);
	putchar(10);
}

int main()
{
	int t, i, top, len;
	Node* root;
	scanf("%d\n", &t);
	while (t--)
	{
		memset(node, 0, sizeof(node));
		gets(s);
		top = 0, len = strlen(s);
		for (i = 0; i < len; ++i)
		{
			node[i].ch = s[i];
			if (islower(s[i]))
				stack[top++] = &node[i];
			else
			{
				node[i].right = stack[--top];
				node[i].left = stack[--top];
				stack[top++] = &node[i];
			}
		}
		root = &node[len - 1];
		bfs(root);
	}
	return 0;
}

ardupilot开发 --- Remote ID 篇 适配ardupilot的Remote ID模块研发,远程标识模块研发。Remote ID模块可以通过蓝牙或wifi向外广播无人机的状态信息、位置信息、任务信息等,并可以通过蜂窝数据向国家无人驾驶航空器一体化综合监管服务平台上报这些信息。Remote ID功能是21年起国家出台的民用无人机适航飞行的基本要求! 阅读详情

相关推荐

HDLbits练习11 - Counter 1-12 ( Exam/ece241 2014 q7a )

(如这题用于当复位信号为高或者计数器递增至12时,该信号置1使计数器的输入信号 ’ d ’ 变为 ’ 4’b0001 ’ ,再加载到 ’ Q ’ 中。当 ’ load ’ 信号为高时,在时钟的上升沿,计数器会将 ’ d ’ 输入的值加载到 ’ Q ’ (计数器的输出)中,而不是递增当前值。用途:在时钟上升沿触发的情况下,如果 ’ enable ’ 信号为高,计数器的值会递增,如果 ’ load ’ 信号为高,计数器将加载 ’ d ’ 信号的值。作用: ’ Q ’ 是计数器的当前值,也是模块的主要输出。

weixin_62830176的博客 473

LeetCode107 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null,15,7

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null,15,7], /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

CourageKang的博客 2981

层层剥开Allegro skill--快速抓取并写入Allegro软件【呕血创作,转载注明】

Allegro skill eda pcb

qq_36492361的博客 1255

LeetCode 初级 - 二叉树的层次遍历

二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层次遍历结果: [ [3], [9,20], [15,7] ] 分析 我们可以通过两个队列分别记录当前层的节点...

whdAlive 's blog 1050

CFturbo 2024保姆级教程:从流量扬程到三维叶轮,手把手搞定离心泵水力设计

本文提供CFturbo 2024离心泵水力设计的全流程实战指南,从流量扬程参数输入到三维叶轮模型导出,详细解析设计步骤与优化技巧。涵盖叶轮尺寸确定、子午面型线设计、叶片角度调整等关键环节,帮助工程师高效完成CAE辅助的离心泵水力设计,提升设计质量与效率。

weixin_42569329的博客 935

铭飞MCMS源码部署

文章目录前言一、下载铭飞源码二、部署源码1.使用IDEA打开源码2.配置tomcat环境2.1下载tomcat并测试是否可以成功启动(此处不赘述)2.2在IDEA中安装tomcat环境并配置3.添加数据库,并配置3.1新建数据库3.2 导入sql配置三、启动测试 前言 铭飞MCMS基于IDEA、tomcat和mysql的环境搭建。 提示:以下是本篇文章正文内容,下面案例可供参考 一、下载铭飞源码 gitee:https://gitee.com/mingSoft/MCMS/releases(各种版本按需下

MARS_ZORO的博客 4066

反层次遍历,自下而上,从右往左遍历二叉树

实现代码: #include &amp;lt;iostream&amp;gt; #include &amp;lt;stdlib.h&amp;gt; #define maxSize 1000 using namespace std; typedef struct BTNode{ int data,r; struct BTNode *lchild,*rchild; }BTNode; //二叉树样式 // 6 // ...

山重水复疑无路,柳暗花明又一村 1459

SAP 批量处理分包事后调整:BAPI_GOODSMVT_CREATE 关键参数与避坑指南

本文详细解析了SAP系统中使用BAPI_GOODSMVT_CREATE进行分包事后批量调整的关键参数配置与常见错误排查。重点介绍了GOODSMVT_CODE设置为'07'的必要性、GOODSMVT_ITEM结构体的正确配置方法,以及如何避免M7384、M7386等常见错误,帮助用户高效处理分包业务中的数量调整问题。

weixin_29276847的博客 568

LintCode 70 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)

/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this-&gt;val = val; * this-&gt;left = this-...

TERMINATOR-FOR-EVER 781

【C语言学习疑难杂症】第20期:0x00007FF7F2821964处引发的异常0xC0000005写入位置 0x0000000000000

检查代码中是否存在野指针访问的情况,确保在进行写入操作之前,内存已经被正确分配或初始化,并且在使用完毕后进行了正确的释放。空指针访问:代码尝试在空指针地址(0x0000000000000000)处进行写入操作,导致访问冲突。检查代码中是否存在数组越界的情况,确保在进行写入操作之前,数组的索引值在合法范围内。检查代码中是否存在空指针访问的情况,确保在进行写入操作之前,指针已经被正确初始化。野指针访问:代码尝试在已释放或未初始化的内存地址进行写入操作,导致访问冲突。

学无止境 828

第二节:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入门 - Python Flask 后端开发详解(附源码)

大家好,我是cuiwin!本节内容,我们从零开始开发基于Python(Flask)的后端项目,对接Vue Vben Admin(vben5)。登录mysql数据库执行: 创建数据库。

cuiwin的专栏 2266

Leetcode—— 102. 二叉树的层次遍历

102. 二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回其层次遍历结果: [ [3], [9,20], [15,7] ] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems...

suxiaorui的博客 307

阅读链接

图像处理链接:http://homepages.inf.ed.ac.uk/rbf/HIPR2/hipr_top.htm

gougouhai1212的专栏 596

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

UVA 11234 Expressions

UVA-11234题意:给出一颗满二叉树的后序遍历,求它从下到上的逐层遍历。 解题思路:利用栈先把二叉树给求出来,在通过队列求出从上到下顺序。反过来输出。/************************************************************************* > File Name: UVA-11234.cpp > Author: Nar

QzNarsh的博客 431

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

UVa11234 - Expressions

题目地址:点击打开链接 二叉树重建+层序遍历 C++代码: #include #include #include #include #include #include using namespace std; struct TreeNode { char data; TreeNode *lchild,*rchild; }; int main() { int n; whi

370

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

给定一个二叉树,返回自下而上的顺序遍历其节点值(即从左到右,从叶到根逐级)。

例如: 3 / \ 9 20 / \ 15 7 返回: [[15,7] [9,20],[3],] 按层遍历 [ [15,7] [9,20], [3], ] vector > levelOrderBottom(TreeNode *root) { vector> result; if(r

i_am_bird的博客 2187

二叉树自下而上,从右到左的层次遍历

#include &amp;quot;stdafx.h&amp;quot; #include&amp;amp;lt;stdio.h&amp;amp;gt; #include&amp;amp;lt;stdlib.h&amp;amp;gt; #include&amp;amp;lt;cstring&amp;amp;gt; #define max 500 struct queue { type member[max]; int tail,head; }; void initqueue(que

vivi_and_qiao的博客 3557

三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明

一、威尔逊定理 若p为质数,则 p|(p-1)!+1 亦:(p-1)! ≡ p-1 ≡ -1(mod p) 例题: HDU 2973 YAPTCHA (威尔逊定理及其逆定理) 解题报告见http://blog.youkuaiyun.com/synapse7/article/details/18728157 二、费马小定理 假如p是质数,且gcd(a,p)=1,那么 a^(

AC,∑ndless 1万+

使用bitset实现二进制和十进制的相互转换

#include #include #include #include #include #include using namespace std; const int Size = 32; const double log_2 = log(2.0); char str[Size]; int main(void) { int num; char *endstr; while (~scan

AC,∑ndless 9683

动态规划解最长公共子序列(LCS)问题 (附可打印LCS完整代码)

动态规划法 经常会遇到复杂问题不能简单地分解成几个子问题,而会分解出一系列的子问题。简单地采用把大问题分解成子问题,并综合子问题的解导出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加。 为了节约重复求相同子问题的时间,引入一个数组,不管它们是否对最终解有用,把所有子问题的解存于该数组中,这就是动态规划法所采用的基本方法。 【问题】 求两字符序列的最长公共字

AC,∑ndless 9136

全排列的编码与解码——康托展开 (附完整代码)

一、康托展开:全排列到一个自然数的双射 X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0! ai为整数,并且0  适用范围:没有重复元素的全排列 二、全排列的编码: {1,2,3,4,...,n}的排列总共有n!种,将它们从小到大排序,怎样知道其中一种排列是有序序列中的第几个? 如 {1,2,3} 按从小到大排列一共6个:

AC,∑ndless 8527

中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期

此题相当于解方程组 x+d≡p(mod 23) x+d≡e(mod 28) x+d≡i(mod 33) 由于23,28,33两两互素,所以M=23*28*33=21252,M1=924,M2=759,M3=644. 则有924y1≡1(mod 23),由924/23=(40,5,1,3)计算得y1=6(mod 23) 则有759y2≡1(mod 28),由759/28=(27,9,2,1)计算得y2=19(mod 28) 则有644y3≡1(mod 33),由644/33=(19,1,1,16)计算得y3=

AC,∑ndless 8344

最长递增子序列(LIS)的O(NlogN)打印算法

题目: 求一个一维数组arr[n]中的最长递增子序列的长度,如在序列1,5,8,3,6,7中,最长递增子序列长度为4 (即1,3,6,7)。 方法一:一般的DP方法(O(N^2)) 像LCS一样,从后向前分析,很容易想到,第i个元素之前的最长递增子序列的长度要么是1(单独成一个序列),要么就是第i-1个元素之前的最长递增子序列加1,这样得到状态方程:

AC,∑ndless 7453

最短路模板:使用priority_queue实现的Dijkstra算法

#include #include #include #include #include #include using namespace std; const int mx = 10005; typedef pair P; ///first是最短距离,second是顶点编号 struct edge { int to, cost; }; vector G[mx]; int disTo[mx]

AC,∑ndless 5007

Manacher 算法详解:O(n) 复杂度求最长回文子串

先预处理下:在每个字符的两边都插入一个特殊的符号,比如abba变成#a#b#b#a#,aba变成#a#b#a#(因为Manacher算法只能处理奇数长度的字符串)。同时,为了避免数组越界,在字符串开头添加另一特殊符号,比如$#a#b#a#。 以字符串3212343219为例,处理后变成S[] = "$#3#2#1#2#3#4#3#2#1#9#"。 然后用一个数组Len[i]来记录以字符S[i

AC,∑ndless 4609
上一篇: POJ 1835 宇航员 (模拟&三维向量旋转)
下一篇: UVa 10562 Undraw the Trees (二叉树先序遍历)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值