POJ3367 HDU1805 UVA11234 Expressions【树】

Expressions
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3796 Accepted: 1098

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?

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

Source

Ulm Local 2007

问题链接POJ3367 HDU1805 UVA11234 Expressions
问题简述:(略)
问题分析
    给定一颗完全二叉树的后序遍历,小写字母表示叶子结点,大写字母表示其他结点,按照从下到上从右到左顺序(按层)输出这棵树。
    构建树,然后按要求遍历输出。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ3367 HDU1805 UVA11234 Expressions */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <queue>

using namespace std;

const int N = 10000;
char exp[N + 1], ans[N + 1];
struct Tree {
    char node;
    int left, right;
} tree[N];
int tcnt;

// 从右到左查找第一个小写字母
int find(int k)
{
    for(int sum = 0; ; k--){
        if(islower(exp[k])){
            if(++sum == 1) break;
        } else
            sum--;
    }
    return k;
}

int buildTree(int start, int end)
{
    int p = tcnt++;
    tree[p].node = exp[end];
    tree[p].left = tree[p].right = -1;
    if(start == end) return p;
    int m = find(end - 1);
    if(m < end)
        tree[p].right = buildTree(m, end - 1);
    if(m > start)
        tree[p].left = buildTree(start, m - 1);
    return p;
}

void lvlTraverse(int p, int len)
{
    queue<Tree> q;
    ans[len] = '\0';
    int idx = len - 1;
    q.push(tree[p]);
    while(!q.empty()){
        Tree t = q.front();
        q.pop();
        ans[idx--] = t.node;
        if(t.left >= 0)
            q.push(tree[t.left]);
        if(t.right >= 0)
            q.push(tree[t.right]);
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    getchar();
    while(t--) {
        gets(exp);
        tcnt = 0;
        int len = strlen(exp);
        lvlTraverse(buildTree(0, len - 1), len);
        printf("%s\n", ans);
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10425637.html