From Prefix to Postfix
InterviewTime limit1sMemory limit128 MB
Translate each prefix arithmetic expression over + and - into its equivalent postfix form, stopping at the terminating 0.
Problem
Prefix notation is an unconventional way of writing arithmetic expressions. In the standard style, called infix notation, a binary operator is placed between its two operands, e.g. 3 + 4. In prefix notation the operator is placed before its operands, e.g. + 3 4. Likewise, the prefix form of 5 - 2 is - 5 2.
A useful property of prefix expressions with binary operators is that parentheses are never needed, because the order of operations is always unambiguous. For example, the prefix form of 5 - (4 - 2) is - 5 - 4 2, while the prefix form of (5 - 4) - 2 is - - 5 4 2. Prefix notation is also known as Polish notation, after the Polish logician Jan Łukasiewicz, who introduced it around 1920.
In postfix notation, also called reverse Polish notation, the operator is placed after its operands. For example, the postfix form of the infix expression (5 - 4) - 2 is 5 4 - 2 -.
Write a program that translates a prefix arithmetic expression into a postfix arithmetic expression.
Input
Each line contains one arithmetic expression written in prefix notation. The only operators are + and -, and every number is a single decimal digit. Operators and numbers are separated by exactly one space, and there are no leading spaces. Each expression is valid and contains fewer than 20 operators. A line containing a single 0 marks the end of the input.
Output
For each expression, output its postfix form on its own line, with tokens separated by a single space. The terminating 0 is not translated.