1751: E: Expressions
Time Limit: 2000 ms Memory Limit: 10000 kB
Total Submit : 86 (18 users) Accepted Submit : 14 (13 users) Page View : 2351
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
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
/* 功能Function Description: 层次遍历(转) 开发环境Environment: DEV C++ 4.9.9.1 技术特点Technique: 版本Version: 作者Author: 可笑痴狂 日期Date: 20120803 备注Notes: 题目来源: http://acm.nankai.edu.cn/p1751.html 给出表达式二叉树的后序遍历,求层序遍历(按照BFS依次访问的顺序) 大写字母表示操作符,小写字母表示操作数 */ //转别人的代码---不会啊 泪奔ing #include <iostream> #include <cctype> #include <queue> #include <string> #include <vector> using namespace std; string str; struct node { char data; bool mark; node*lch,*rch; }; node *root=NULL; node tree[10005]; //树的层次遍历过程 void bfs(int n) { node *root=&tree[n-1]; queue<node*> q;//结点指针的队列 vector<char> v(n);//放结果的数组 int t=0; node* tmp; q.push(root); while(!q.empty()) { v[t++]=q.front()->data; tmp=q.front(); q.pop(); if(tmp->lch)q.push(tmp->lch); if(tmp->rch)q.push(tmp->rch); } for(int i=n-1;i>=0;i--)cout<<v[i]; cout<<endl; } void creat(string str) { int len=str.size(); for(int i=0;i<len;i++){tree[i].data=str[i];tree[i].lch=tree[i].rch=NULL;tree[i].mark=0;} int pos=0,t=0,n=len; while(n!=1) { while(islower(str[pos])){t++;pos++;} int i=t-1; while(tree[i].mark)i--; tree[t].rch=&tree[i]; tree[i].mark=1;i--; while(tree[i].mark)i--; tree[t].lch=&tree[i]; tree[i].mark=1; n-=2; str.erase(pos-2,2); t++; pos--; } bfs(len); } int main() { int icase; cin>>icase; while(icase--) { cin>>str; creat(str); } // system("pause"); return 0; }