24h購物| | PChome| 登入
2012-11-18 11:49:52| 人氣1,985| 回應0 | 上一篇 | 下一篇

[UVA][建樹] 11234 - Expressions

推薦 0 收藏 0 轉貼0 訂閱站台

2007/2008 ACM International Collegiate Programming Contest
University of Ulm Local Contest

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:

  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 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


這題一點也不難,題目要看懂真的很難,小寫是運算元,大寫是運算子。
給一個 post-order 輸出 level-order。
使用遞迴建樹,之後用 queue 找到 level-order,也差不多類似 bfs。


#include <stdio.h>
#include <string.h>
char post[10005], tree[10005];
int node[10005][2];
int idx, size;
void set(int nd) {
tree[nd] = post[idx--];
if(tree[nd] >= 'a' && tree[nd] <= 'z')
return;
node[nd][1] = ++size;
set(size);
node[nd][0] = ++size;
set(size);
}
int main() {
int t, i;
scanf("%d", &t);
gets(post);
while(t--) {
gets(post);
idx = strlen(post)-1;
size = 1;
set(1);
int qu[10005], qidx = 0;
qu[0] = 1;
for(i = 0; i <= qidx; i++) {
if(tree[qu[i]] >= 'a' && tree[qu[i]] <= 'z')
continue;
qu[++qidx] = node[qu[i]][0];
qu[++qidx] = node[qu[i]][1];
}
for(i = qidx; i >= 0; i--)
putchar(tree[qu[i]]);
puts("");
}
return 0;
}
 

台長: Morris
人氣(1,985) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dp][最優二叉樹] 12057 - Prefix Codes
此分類上一篇:[UVA][ST] 12532 - Interval Product

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文