Expressions
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 201 Accepted Submission(s): 102
Problem Description
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:
1. push: a number is inserted at the top of the stack.
2. 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:
1. push: a number is inserted at the end of the queue.
2. 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?
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:
1. push: a number is inserted at the top of the stack.
2. 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:
1. push: a number is inserted at the end of the queue.
2. 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
解题思路:
从图中我们可以发现output
第一次可恶的超时了:
主要是创建数组用的二叉链表,
typedef struct node { char data; node * lchild; node * rchild; }node;
还有队列用的STL标准模板,不知道为什么这也会很费时间;;;;==!
#include <iostream> #include <stack> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; typedef struct node { char data; node * lchild; node * rchild; }node; stack <node *> S; //int k; //char Pnt[10002]; bool is_upper(char c) { if(c>='A' && c<='Z') return true; return false; } node * create_tree(char s[]) { node * p; node *root; int i; for(i=0;i<strlen(s);i++) { if(is_upper(s[i])) { p = (node*)malloc(sizeof(node)); p->data = s[i]; p->rchild = S.top(); S.pop(); p->lchild = S.top(); S.pop(); root = p; S.push(p); } else { p = (node*)malloc(sizeof(node)); p->data = s[i]; p->lchild = NULL; p->rchild = NULL; S.push(p); } } return root; } void BFS(node * root) { queue <node *> Q; Q.push(root); while(!Q.empty()) { printf("%c",Q.front()->data); if(Q.front()->lchild) Q.push(Q.front()->lchild); if(Q.front()->rchild) Q.push(Q.front()->rchild); //Pnt[k++] = Q.front()->data; Q.pop(); } } /* 2 xyPzwIM abcABdefgCDEF */ int main() { int t,i;char s[10002]; node *root; scanf("%d",&t); getchar(); while(t--) { gets(s); root = create_tree(s); BFS(root); printf("\n"); } // system("pause"); return 0; }
f第二次:南开oj 通过,因为限时2000MS , 改进代码时间 1500MS ,但是对于杭电oj,POJ都不行,还超时,
其中放弃二叉链表创建数组,而是用一个 结构体数组,存放孩子信息,
#include <iostream> #include <stack> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; typedef struct TREE { char dat; int l,r; }TREE; TREE tree[10002]; stack <int> S; int k; char Pnt[10002]; bool is_upper(char c) { if(c>='A' && c<='Z') return true; return false; } void create_tree(char s[]) { node * p; node *root; int i; for(i=0;i<strlen(s);i++) { tree[i].dat=s[i]; if(is_upper(s[i])) { tree[i].r = S.top(); S.pop(); tree[i].l = S.top(); S.pop(); S.push(i); // p = (node*)malloc(sizeof(node)); // p->data = s[i]; // p->rchild = S.top(); S.pop(); // p->lchild = S.top(); S.pop(); // // root = p; // S.push(p); } else { tree[i].r = -1; tree[i].l = -1; S.push(i); // p = (node*)malloc(sizeof(node)); // p->data = s[i]; // p->lchild = NULL; // p->rchild = NULL; // S.push(p); } } // return root; } void BFS(char s[]) { queue <int> Q; Q.push(strlen(s)-1); while(!Q.empty()) { Pnt[k++] = tree[Q.front()].dat; // printf("%c",tree[Q.front()].dat); if(tree[Q.front()].l != -1) Q.push(tree[Q.front()].l); if(tree[Q.front()].r != -1) Q.push(tree[Q.front()].r); Q.pop(); } } /* 2 xyPzwIM abcABdefgCDEF */ int main() { int t,i;char s[10002]; node *root; scanf("%d",&t); getchar(); while(t--) { k=0; gets(s); create_tree(s); BFS(s); // printf("\n"); Pnt[k]='\0'; for(i=strlen(Pnt)-1;i>=0;i--) printf("%c",Pnt[i]); printf("\n"); } // system("pause"); return 0; }
第三次改进: 就放弃STLqueue 用一个一位数组表示队列,(我不知道是不是这里的原因,,,时间一下子减少不少),如果有大神知道的话,混应知道
time: 20MS,,这次到哪里都可以AC,无压力了;;;
#define N 10005 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct TREE { int l,r,own; }TREE; TREE tree[N]; char Pnt[N],s[N]; int Stack[N],Q[N]; void build_tree(int n) { int i,top=-1; memset(tree,-1,sizeof(tree)); for(i=0;i<n;i++) { if(s[i]>='a'&&s[i]<='z')//小写 Stack[++top]=i; else{ tree[i].r = Stack[top--]; tree[i].l = Stack[top]; Stack[top]= i; } tree[i].own = i; } } void BFS(int n) { int front=0,rear=1,k=0; for(Q[0]=n;front<rear;front++) { TREE u = tree[Q[front]]; // printf("front=%d,c=%c\n",front,s[u.own]); Pnt[k++]= s[u.own]; if(u.l != -1) Q[rear++] = u.l; if(u.r != -1) Q[rear++] = u.r; } } /* 2 xyPzwIM abcABdefgCDEF */ int main() { int t,i,k; scanf("%d",&t); getchar(); while(t--) { gets(s); // printf("len=%d\n",strlen(s)); int n = strlen(s); build_tree(n); BFS(n-1); for(i=n-1;i>=0;i--) printf("%c",Pnt[i]); printf("\n"); } // system("pause"); return 0; }