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:
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:
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?
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.
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.
2 xyPzwIM abcABdefgCDEF
wzyxIPM gfCecbDdAaEBF
给出后缀表达式,求出一个表达式用队列操作之后,使他们的中缀表达式相同;
没思路百度之后,先构建表达式树再按层次遍历即可。恍然大悟~~~~(>_<)~~~~
第一次写层次遍历老是自然而然朝dfs写悲剧。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tree
{char data;
struct tree *left,*right;
};
char s[10001],stack[100001];
int top,l;
int ok(char x)
{if ((x>='a')&&(x<='z')) return 1; return 0;};
int main()
{
int i,j,k,t;
struct tree *p,*q,*r,*root,*link[10001],*point[10001];
scanf("%d\n",&t);
while (t--)
{
gets(s); top=0;
l=strlen(s);
for (i=0;i<l;i++)
{if (ok(s[i])) {++top; stack[top]=s[i];}
else
{
p=(struct tree*)malloc(sizeof(struct tree));
p->data=s[i];
if (ok(stack[top]))
{q=(struct tree*)malloc(sizeof(struct tree));
q->data=stack[top];
q->left=NULL;
q->right=NULL;
}
else q=link[top];
if (ok(stack[top-1]))
{r=(struct tree*)malloc(sizeof(struct tree));
r->data=stack[top-1];
r->left=NULL;
r->right=NULL;
}
else r=link[top-1];
p->left=r;
p->right=q;
--top;
link[top]=p;
stack[top]=s[i];
root=p;
}
}
top=1; j=1; link[1]=root;
s[top]=root->data;
while (top<l)
{k=0;
for (i=1;i<=j;i++)
{if (link[i]->left!=NULL) {++k;++top; point[k]=link[i]->left ;s[top]=point[k]->data;}
if (link[i]->right!=NULL) {++k;++top; point[k]=link[i]->right;s[top]=point[k]->data;}
}
j=k;
for (i=1;i<=j;i++)
link[i]=point[i];
}
for (i=l;i>=1;i--)
printf("%c",s[i]);
printf("\n");
}
return 0;
}