CSU——2152: 小Z的表达式

Description

逆波兰表达式 (Reverse(Reverse PolishPolish Notation,RPN)Notation,RPN) 是一种由波兰数学家 JanJan ŁukasiewiczŁukasiewicz 于 19201920 年引入的数学表达式方式,在逆波兰记法中,所有操作符置于操作数的后面,因此也被称为后缀表示法。逆波兰记法不需要括号来标识操作符的优先级。

逆波兰结构在 2020 世纪 6060 年代早期由 FriedrichFriedrich L.L. BauerBauer 和 EdsgerEdsger W.W. DijkstraDijkstra 独立改造并提议用于表达式求值。澳大利亚哲学家和计算机科学家 CharlesCharles L.L. HamblinHamblin 在 2020 世纪 5050 年代中期扩展了该方案的算法和符号。

一个一元运算使用逆波兰记法的例子是阶乘的记法: n!n!

在逆波兰记法中,操作符置于操作数的后面。例如表达“三加四”时,写作 “3“3 4+”4+” , 而不是 “3+4”“3+4” 。 如果有多个操作符,操作符置于第二个操作数的后面,所以常规中缀记法 “3−4+5”“3−4+5” 在逆波兰记法中写作 “3“3 4−5+”4−5+” , 使用逆波兰记法的一个好处是不需要使用括号。

对于二元表达式,我们可以对其建立一棵二叉树,,以基本运算对象作为叶结点中的数据,以运算符作为非叶节点中的数据,其两棵子树是它的运算对象,子树可以是基本运算对象,也可以是复杂表达式。如图就是一棵表达式树。对表达式树进行后序遍历(先遍历左子树,再遍历右子树,最后访问根节点)就可以得到逆波兰表达式。

 

现在小 Z 得到了若干棵表达式树,但他不会转换为逆波兰表达式并求值,所以他想求助于你,请你帮助他将这些表达式树转换为逆波兰表达式,并计算出其对应的值。

Input

第一行 TT ,代表数据组数(不超过 100100 组)

对于每组数据

首先一个 nn ,代表表达式树的节点个数( 1≤n≤1000001≤n≤100000 )

接下来 nn 行,每行 xx , yy , zz ,第 ii 行代表对第 ii 个节点的描述,11 代表根节点

x=0x=0 代表这个节点是运算符, yy 是 “+”、“-”、“*”、“/” 四种运算符中的一个

x=1x=1 代表这个节点是操作数, yy 是其值, y∈[−1000,1000]y∈[−1000,1000]

zz 代表该节点的父亲编号,根节点的父亲编号为 00;

对于操作符的子树,输入中先出现的为其左子树,后出现的为其右子树。

数据保证运算中间过程和结果属于 [−231,231−1][−231,231−1],且合法的除法均为整除

Output

对每一组数组,输出两行

第一行为其表达式树的逆波兰表达式(以空格分隔)

第二行为其对应的值,如果运算中出现除以0的非法情况,则输出“ERROR”(不含引号)

具体参见样例输出

Sample Input

2
11
0 - 0
0 + 1
1 7 2
0 * 2
1 8 4
0 / 1
1 6 6
1 3 6
0 - 4
1 5 9
1 2 9 
5
0 / 0
1 3 1
0 - 1
1 6 3
1 6 3

Sample Output

7 8 5 2 - * + 6 3 / -
29
3 6 6 - /
ERROR

这题是比赛的签到题,但是时间都花在D上了,这题就是一道模拟我觉得,先建树,然后后序遍历去即可,基本上建树的过程和Tire树的建树感觉差不多。

然后就是后序遍历,开个stack存储就行了。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=100005;
const double eps=1e-8;
const double pi=acos(-1.0);
const int MOD=10056;

int T,n;
struct Node
{
    int lson,rson;
    int flag,num;
    char op[2];
};
int p;
Node node[maxn];
stack<int>s;
void init()
{
    while(!s.empty())
        s.pop();
    for(int i=1;i<=n;i++)
    {
        node[i].lson=-1;
        node[i].rson=-1;
    }
    p=1;
}
int fun(int a,int b,char op)
{
    if(op=='-')
        return a-b;
	if(op=='+')
        return a+b;
	if(op=='*')
        return a*b;
	if(op=='/')
    {
        if(b==0)
            return INF;
        else
            return a/b;
    }
}
void dfs(int x)
{
    if(node[x].lson!=-1)
        dfs(node[x].lson);
    if(node[x].rson!=-1)
        dfs(node[x].rson);
    if(!node[x].flag)
    {
        printf("%c ",node[x].op[0]);
		int a=s.top();s.pop();
		int b=s.top();s.pop();
		int	num=fun(b,a,node[x].op[0]);
		if(num==INF)
            p=0;
		s.push(num);
    }
    else
    {
        printf("%d ",node[x].num);
        s.push(node[x].num);
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i=1;i<=n;i++)
        {
            int t;
            scanf("%d",&node[i].flag);
            if(node[i].flag==0)
                scanf("%s",node[i].op);
            else
                scanf("%d",&node[i].num);
            scanf("%d",&t);
            if(!t)
                continue;
            if(node[t].lson!=-1)
                node[t].rson=i;
            else
                node[t].lson=i;
        }
        int sum=0;
        dfs(1);
        printf("\n");
        if(!p)
        {
            printf("ERROR\n");
            continue;
        }
        sum=s.top();
        s.pop();
        printf("%d\n",sum);
    }
    return 0;
}

/**********************************************************************
	Problem: 2152
	User: jk1601zr
	Language: C++
	Result: AC
	Time:4728 ms
	Memory:3976 kb
**********************************************************************/

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值