ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)
2014-12-14 12:06
483 查看
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest
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:
push: a number is inserted at the top of the stack.
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:
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:
push: a number is inserted at the end of the queue.
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?
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest
Problem E: Expressions
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:
push: a number is inserted at the top of the stack.
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:
push: a number is inserted at the end of the queue.
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 根据题目意思,运算符是二元的,故想到使用二叉树结构来存放所有元素。 根据题目意思,读入二叉树的过程是一个后序遍历的过程,故使用题目描述中的栈结构进行建树。 根据题目意思,输出过程是从树的最下层网上,一层层将树输出。 考虑到此处使用的是指针,故没有使用STL里面的队列,构造了Queue类,使用了循环队列。 代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <string> #define inf 0x3fffffff #define eps 1e-10 using namespace std; struct node { char val; node *left; node *right; }; struct Queue { node *v[10005]; int top; int rear; const int N = 10005; void Init() { top = 0; rear = 0; } void Pop() { top = (top+1) % N; } node *Front() { return v[top]; } void Push(node * a) { v[rear] = a; rear = (rear+1) % N; } bool Empty() { if (top != rear) return 0; else return 1; } }q; node *head, *Stack[10005]; int top; node *Create() { top = 0; char ch; for (;;) { ch = getchar(); if (ch == '\n') { return Stack[0]; } if (ch >= 'a' && ch <= 'z') { node *k; k = (node *)malloc(sizeof(node)); k->val = ch; k->left = NULL; k->right = NULL; Stack[top++] = k; } else { node *k; k = (node *)malloc(sizeof(node)); k->val = ch; k->left = Stack[top-2]; k->right = Stack[top-1]; top -= 2; Stack[top++] = k; } } } void bfs() { q.Init(); q.Push(head); top = 0; while (!q.Empty()) { Stack[top] = q.Front(); q.Pop(); if (Stack[top]->left != NULL) { q.Push(Stack[top]->left); q.Push(Stack[top]->right); } top++; } } void Output() { for (int i = top-1; i >= 0; --i) printf("%c", Stack[i]->val); printf("\n"); } int main() { //freopen ("test.txt", "r", stdin); int T; scanf("%d", &T); getchar(); for (int times = 1; times <= T; ++times) { head = Create(); bfs(); Output(); } return 0; }
相关文章推荐
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
- ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)
- ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)
- ACM学习历程19——queue队列容器与priority_queue优先队列容器
- ACM学习历程—HDU 1276 士兵队列训练问题(队列)
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
- ACM学习历程—HDU2222 Keywords Search(字典树)
- ACM学习历程——POJ 1700 Crossing River(贪心)
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
- ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)
- 算法学习 - 树的三种遍历(递归实现)先序遍历,中序遍历,后序遍历
- ACM学习历程—Rotate(HDU 2014 Anshan网赛)(几何)
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
- 数据结构学习笔记-二叉树的前、中、后序遍历,递归、非递归方式
- uva 11234 Expressions 表达式 建树+BFS层次遍历
- ACM学习历程—HDU4969 Just a Joke(物理题)