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
题目大意:给你一串字符,小写表示数字,大写字符表示运算符,碰到小写字符则进盏,碰到一个大写字符后,将前面两个数字出栈做一下运算后结果再压入栈中,直到结束。现在要求使用队列得到相同的运算结果,要求你求出应该按什么顺序输入。
输入:第一行一个整数t表示组数,接下来t行,为使用栈进行运算的输入序列。
输出:输出使用队列得到相同结果应该的输入序列。
分析:我们模拟栈进行运算建立一颗树,可以发现所要求的结果就是这颗树的层序遍历的逆序。
现在我们通过输入来建立一个树。碰到小写字母时新建一个结点存储,压入栈中,碰到大写字母时弹出两个结点并新建结点存储这个大写字母,将其左右孩子指向弹出的两个结点,再入栈。树建立完成后,只要对其进行层序遍历然后逆序输出即可。
1 #include<iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 #include <queue> 6 using namespace std; 7 struct node 8 { 9 char num; 10 node *left,*right; 11 node(char c,node *l,node *r) 12 { 13 num=c; 14 left=l; 15 right=r; 16 } 17 node(char c) 18 { 19 num=c; 20 left=NULL; 21 right=NULL; 22 } 23 }; 24 char str[10010]; 25 stack<node *> s; 26 int main () 27 { 28 int t; 29 scanf("%d",&t); 30 gets(str); 31 while(t--) 32 { 33 gets(str); 34 int n=strlen(str); 35 for(int i=0; i<n; ++i) 36 { 37 if(str[i]>='a'&&str[i]<='z') 38 { 39 node *newnode=new node(str[i]); 40 s.push(newnode); 41 } 42 else if(str[i]>='A'&&str[i]<='Z') 43 { 44 node *a=s.top(); 45 s.pop(); 46 node *b=s.top(); 47 s.pop(); 48 node *root=new node (str[i],b,a); 49 s.push(root); 50 } 51 } 52 node *root=s.top(); 53 s.pop(); 54 queue<node *>q; 55 q.push(root); 56 while(!q.empty()) 57 { 58 node *p=q.front(); 59 q.pop(); 60 if(p->left!=NULL) 61 q.push(p->left); 62 if(p->right!=NULL) 63 q.push(p->right); 64 s.push(p); 65 } 66 while(!s.empty()) 67 { 68 printf("%c",s.top()->num); 69 s.pop(); 70 } 71 cout<<endl; 72 } 73 }