UVa 11234 Expressions
2013-11-16 15:28
651 查看
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:
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.
题目讲了一大堆栈的队列的什么,也没读懂,百度题意知道大致意思是:给你一个后序顺序创建的树,让你从下往上,从左到右层序遍历(正序遍历保存到数组中,再逆序输出)。搞了n久还是不会做。。。实在是用不好指针队列!!!
后来看了大神的代码知道可以用BFS遍历法。
题目代码如下:
另外一定牢记下面这个算法!!!!!!!!!!!!!
以后再遇到层序遍历就可以不用队列了,哈哈!!
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
题目讲了一大堆栈的队列的什么,也没读懂,百度题意知道大致意思是:给你一个后序顺序创建的树,让你从下往上,从左到右层序遍历(正序遍历保存到数组中,再逆序输出)。搞了n久还是不会做。。。实在是用不好指针队列!!!
后来看了大神的代码知道可以用BFS遍历法。
题目代码如下:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> const int max=10010; typedef struct Tree { char data; struct Tree *lchild,*rchild; }tree; tree *newnode(void) { tree *u=(tree*)malloc(sizeof(tree)); if(u!=NULL) { u->data='\0'; u->lchild=u->rchild=NULL; } return u; } int len,cur; char str[max],ch; void buildtree(tree *p) { if(cur<0) return ; p->data=str[cur--]; if(isupper(p->data)) { if(p->rchild==NULL) p->rchild=newnode(); buildtree(p->rchild); if(p->lchild==NULL) p->lchild=newnode(); buildtree(p->lchild); } } int main(void) { int n,i; tree *root,*q[max]; while(scanf("%d",&n)!=EOF) { while(n--) { scanf("%s",str); len=strlen(str); cur=len-1; root=newnode(); buildtree(root); int rear=0,front=0; q[rear++]=root; while(rear>front) { if(q[front]) { if(q[front]->lchild) q[rear++]=q[front]->lchild; if(q[front]->rchild) q[rear++]=q[front]->rchild; front++; } else front++; } for(int i=rear-1;i>=0;i--) printf("%c",q[i]->data); printf("\n"); //用数组实现从右到左,从下往上的层序遍历 } } return 0; }
另外一定牢记下面这个算法!!!!!!!!!!!!!
以后再遇到层序遍历就可以不用队列了,哈哈!!
int rear=0,front=0; q[rear++]=root; while(rear>front) { if(q[front]) { if(q[front]->lchild) q[rear++]=q[front]->lchild; if(q[front]->rchild) q[rear++]=q[front]->rchild; front++; } else front++; }
相关文章推荐
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
- UVA 11234 Expressions(数据结构,二叉树,栈,队列)
- uva11234 - Expressions
- UVa 11234 - Expressions
- UVa 11234 - Expressions
- UVa-11234-Expressions
- uva 11234 Expressions(建立二叉树+层次遍历)
- UVA11234 Expressions
- UVa-11234-Expressions
- UVa 11234 Expressions
- UVA 11234 Expressions
- UVA11234 - Expressions
- UVaOJ11234---Expressions
- UVA - 11234 - Expressions (栈和队列)
- UVa 11234 - Expressions
- UVa 11234 - Expressions
- UVa 11234 Expressions
- UVA 11234 Expressions
- UVA 11234 - Expressions
- UVA 11234 - Expressions