Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2591 | Accepted: 850 |
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:
- 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:
1 2 3 |
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
1 2 3 |
2 xyPzwIM abcABdefgCDEF |
Sample Output
1 2 |
wzyxIPM gfCecbDdAaEBF |
Source
基本数据结构题, 二叉树+bfs, 温习一下基本算法.
一开始用的数组, 结果自己的程序跑1.5s,最近提交一大批0.1s不到的, 后来就换指针, 换stack,结果还是1.5s, 最后把cout换成printf过后速度瞬间快了20倍, 知道真相的我眼泪掉下来Orz.
PS: 这道题目如果用了动态分配内存(包括STL)的话POJ上面的G++是会超时的, 就算是O(n)的算法…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
/* * File Name: uvaoj11234.cpp * Author: razrLeLe * Mail: razrlele@outlook.com * Homepage: https://yueyu.io * Created Time: 2014年12月26日 星期五 20时50分45秒 */ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 10010; struct node{ char c; node* l; node* r; }tree[maxn]; char in[maxn]; node* q[maxn]; int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int t; cin >> t; while(t--) { stack<node*> s; cin >> in; int root = 0; int len = strlen(in); for(int i = 0; i < len; i++) { if(islower(in[i])) { tree[root].c = in[i]; tree[root].l = NULL; tree[root].r = NULL; s.push(&tree[root]); root++; } else { tree[root].c = in[i]; tree[root].r = s.top(); s.pop(); tree[root].l = s.top(); s.pop(); s.push(&tree[root]); root++; } } int front = 0, rear = 0; node* p = &tree[root-1]; node* p1; q[rear++] = p; //bfs while(rear != front) { p1 = q[front++]; if(p1->l != NULL) q[rear++] = p1->l; if(p1->r != NULL) q[rear++] = p1->r; } for(int i = rear-1; i >= 0; i--) printf("%c", q[i]->c); cout << endl; } return 0; } |