hdu 1805Expressions(二叉树构造的后缀表达式)
Expressions
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 253 Accepted Submission(s): 121
Problem Description
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
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
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
Source
代码:
1 //#define LOCAL
2 #include<cstdio>
3 #include<stack>
4 #include<cstring>
5 #include<cctype>
6 #include<queue>
7 using namespace std;
8 const int maxn=10005;
9 char ss[maxn];
10 char ans[maxn];
11 stack<int>op;
12 struct node
13 {
14 char da;
15 int lef; //该节点的左孩子
16 int rig; //该节点的有孩子
17 }str[maxn];
18 void bfs(int pos)
19 {
20 queue<int>sac;
21 sac.push(pos);
22 int tt=0;
23 while(!sac.empty())
24 {
25 int i=sac.front();
26 ans[tt++]=str[i].da;
27 sac.pop();
28 if(str[i].lef!=str[i].rig)
29 {
30 sac.push(str[i].lef);
31 sac.push(str[i].rig);
32 }
33 }
34 while(pos>=0)
35 printf("%c",ans[pos--]);
36 }
37 int main()
38 {
39 #ifdef LOCAL
40 freopen("test.in","r",stdin);
41 #endif
42 int cas,i;
43 scanf("%d",&cas);
44 while(cas--)
45 {
46 scanf("%s",ss);
47 for(i=0;ss[i];i++)
48 {
49 if(islower(ss[i]))
50 {
51 op.push(i);
52 str[i].da=ss[i];
53 str[i].lef=str[i].rig=-1; //没有孩子
54 }
55 else
56 {
57 str[i].da=ss[i];
58 str[i].rig=op.top();
59 op.pop();
60 str[i].lef=op.top();
61 op.pop();
62 op.push(i);
63 }
64 }
65 bfs(i-1);
66 puts("");
67 }
68 return 0;
69 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 实例解析php的数据类型
- 实现PHP中session存储及删除变量
- php微信公众号开发之秒杀
- php fread函数使用方法总结
- Yii2框架控制器、路由、Url生成操作示例
- Laravel框架实现调用百度翻译API功能示例
- phpstudy2018升级MySQL5.5为5.7教程(图文)
- laravel实现简单用户权限的示例代码
- tp5(thinkPHP5框架)时间查询操作实例分析
- PHP使Laravel为JSON REST API返回自定义错误的问题
- 详解PHP PDO简单教程
- Python实现ElGamal加密算法的示例代码
- PHP实现基于状态的责任链审批模式详解
- django rest framework使用django-filter用法
- 通过实例解析python创建进程常用方法