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

RT-DETR训练自己的数据集(从代码下载到实例测试) 在Windows10上配置CUDA环境教程是百度提出的一种实时目标检测模型。RT-DETR采用了与DETR相同的编码器和解码器结构,但对其进行了大量的优化,在保持较高检测精度的同时,实现了实时目标检测,为目标检测领域提供了一种新的有效解决方案,具有广泛的应用前景,如自动驾驶、智能监控、机器人等领域。以上是RT-DETR官方的论文和代码,详细介绍了RT-DETR的构成和实现。在YOLOv10中也实现了RT-DETR的代码,所以本文使用YOLOv10的项目文件进行RT-DETR的训练和测试。YOLOv10。 阅读详情

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;
}

详解AUTOSAR:专栏总述 本专栏系统介绍AUTOSAR规范及车用控制器软件开发,内容覆盖基础架构、SWC/RTE、通信栈、诊断、存储、安全、看门狗、工具链、环境配置与项目实战,并持续更新各模块的集成机制与工程要点。 阅读详情

相关推荐

从TCGA官网获取新版count数据并精准转换为TPM的实战指南

本文详细介绍了从TCGA官网获取新版count数据并精准转换为TPM的实战指南。通过TCGAbiolinks工具下载原始count数据,结合GENCODE v36注释文件计算基因有效长度,最终实现count到TPM的标准化转换,确保数据完整性和分析可靠性。文章包含完整代码示例和常见问题解决方案,特别适合生物信息学研究人员参考。

weixin_27310417的博客 284

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

高斯投影坐标正反算算法(python)

高斯坐标正反算算法,在正算的时候,y的第一个数字表示是用几度带换算的,第二个数字和第三个数字表示带号。该程序是根据武汉大学出版社出版的第三版的《大地测量学基础》编写, 用于课程实验学习。

weixin_64338372的博客 4588

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

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

whdAlive 's blog 1050

Nordic nRF52832实战:手把手教你搭建智能手环BLE通信框架(附完整代码)

本文详细介绍了如何使用Nordic nRF52832芯片搭建智能手环的BLE通信框架,涵盖芯片优势、协议栈初始化、低功耗广播策略及健康数据服务实现。通过完整代码示例和架构设计,帮助开发者快速掌握蓝牙低功耗通信技术,适用于可穿戴设备开发。

weixin_29192141的博客 247

保姆级教程:手把手教你为裕泰微YT8521 PHY芯片编写Linux设备树(DTS)节点与驱动

本文提供了一份详细的保姆级教程,手把手教你为裕泰微YT8521 PHY芯片编写Linux设备树(DTS)节点与驱动。从基础配置到RGMII接口的特殊设置,再到驱动开发的关键步骤和调试技巧,帮助开发者快速掌握PHY芯片的移植与开发。

weixin_33734785的博客 333

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

实现代码: #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

逆变器技术解析:DDSRF双解耦在正负序分离中的关键作用

本文深入解析了DDSRF双解耦技术在逆变器正负序分离中的关键作用。通过建立正反旋转坐标系并利用交叉反馈解耦网络,DDSRF能快速、精准地分离电网电压中的正负序分量,从而显著提升锁相环在电网不平衡条件下的精度与系统稳定性,是现代高性能并网逆变器的核心算法之一。

m3n5b7v8c9x的博客 411

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

2025数学建模美赛C题完整建模思路——奥运会奖牌榜的模型

2025数学建模美赛C题完整建模思路——奥运会奖牌榜的模型

Yilu264的博客 1万+

智慧城市中的CV:交通流量监控与行人检测

智慧城市中的CV:交通流量监控与行人检测 ,人工智能,计算机视觉,大模型,AI,计算机视觉作为人工智能的重要分支,通过分析和理解图像或视频数据,为智慧城市提供了强大的感知能力。在交通管理领域,计算机视觉技术可实现交通流量监控、行人检测、违章行为识别等功能,助力构建高效、安全、绿色的城市交通系统。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 5万+

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

信捷PLC XD5系列运动控制(10轴)编程应用篇

项目PLC采用XD5-60T10-E,36DI 24DO 晶体管输出,支持10路脉冲输出功能。TMR指令定时器时基可以自己设定,这点比其他品牌PLC方便。ZRN指令我们需要上升沿指令触发。 5、变址寻址 该说不说这个变址寻址是真的强,比西门子指针、三菱的变址寄存器Z 、V都要更加灵活。 7、特殊寄存器:时钟 8、如何建立PLC通讯...............

RXXW_Dor的博客 1万+

阅读链接

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

gougouhai1212的专栏 596

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

数学建模模型总结

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

UVA 11234 Expressions

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

QzNarsh的博客 431

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

例如: 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、付费专栏及课程。

余额充值