UVA 11234 - Expressions
2012-09-24 23:19
447 查看
Description
2007/2008 ACM International Collegiate Programming Contest
University of Ulm Local Contest
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:
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?
by uppercase letters, numbers are represented by lowercase letters. You may assume that the length of each expression is less than 10000 characters.
are associative or commutative.
2007/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 representedby 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 operatorsare associative or commutative.
Sample Input
2 xyPzwIM abcABdefgCDEF
Sample Output
wzyxIPM gfCecbDdAaEBF
规则:遇到数字push进线性表中,遇到操作符,从线性表中pop()出两元素,计算后再push进去。
这个题的意思是让你求一表达示,使用队列按照上面规则操作得到的结果与给定的字符串用栈按上面规则操作得到的结果相同。
刚开始看到这个题一点思路也没有。后来到网上查了下,说是要建树,后来就明白了。
首先利用后辍表达式建好树,然后宽度优先遍历就行了。
language:c++
code:
#include<iostream> #include<vector> #include<string> #include<stack> #include<queue> #include<cctype> using namespace std; struct node { char data; node* left,*right; node(char d=0,node*l=0,node*r=0):data(d),left(l),right(r){} }; node *build(char data,node* left,node* right) { node*father=new node(data,left,right); return father; } int main() { string s; int cas; cin>>cas; while(cas--) { cin>>s; stack<node*>stck; queue<node*>que; string ans; for(size_t i=0;i!=s.size();i++) { if(islower(s[i])) { node* tree=new node(s[i],0,0); stck.push(tree); } else { node* r=stck.top();stck.pop(); node* l=stck.top();stck.pop(); stck.push(build(s[i],l,r)); } } que.push(stck.top()); while(!que.empty()) { node* cur=que.front();que.pop(); ans+=cur->data; if(cur->left)que.push(cur->left); if(cur->right)que.push(cur->right); } for(int i=ans.size()-1;i>-1;i--) cout<<ans[i]; cout<<endl; } return 0; }
相关文章推荐
- uva 11234 Expressions
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
- uva 11234 - Expressions
- UVa 11234 - Expressions
- Uva 11234 - Expressions//数据结构,二叉树
- UVa 11234 Expressions解题报告
- UVA - 11234 Expressions
- UVA - 11234 Expressions
- UVa 11234 Expressions
- uva 11234 Expressions(后缀变层次)
- UVa 11234 - Expressions
- UVa 11234 - Expressions
- uva-11234 Expressions
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
- uva11234 - Expressions
- UVA - 11234 Expressions
- UVA 11234 Expressions(数据结构,二叉树,栈,队列)
- UVa 11234 - Expressions
- UVa-11234-Expressions
- uva 11234 Expressions(建立二叉树+层次遍历)