# Combinatory logic

### Osnovne informacije

Omejitve
• Čas: 4 s
• Spomin: 128 MB
Avtor:
• Vid Kocijan
• UPM 2018

### Pošlji rešitev

Source: (Auto) text C C++ Pascal Java Python 2 Python 3 Perl C# Prolog (SWI) Ruby

Tvoji rezultati.
 Nisi poslal še nobene rešitve.
Več »

Combinatory logic is a simple language that is used for descibing computational expressions. An expression consists of parentheses, variables and operators S$S$ and K$K$. In this task we will consider only those expressions that do not contain variables. Write a program that will read in an expression in combinatory logic, reduce it (i.e. evaluate it as much as possible) and output the whole procedure.

The input contains a string consisting of letters S$S$ and K$K$ and validly-nested parentheses. Operators S$S$ and K$K$ act on other operators and expressions that follow them in the following way:

• Kxy = x$Kxy = x$,
• Sxyz = xz(yz)$Sxyz = xz(yz)$,

where x$x$, y$y$ and z$z$ represent some expressions. In an expression with multiple operators the evaluation is done from left to right. Examples of evaluation can be found in sample tests below.

The expression will not contain redundant parentheses. Parentheses are redundant if their removal does not change the meaning of the expression. In particular, we can always remove parenthesis that enclose a single symbol, e.g. expression (S)$(S)$ becomes S$S$. Moreover, we can always remove left-aligned parentheses, e.g. (((SK)K)S)S$(((SK)K)S)S$ becomes SKKSS$SKKSS$. Another example: Some parentheses in the expression (S((KK)S(K)))$(S((KK)S(K)))$ can be removed so that the expression becomes S(KKSK)$S(KKSK)$.

If the operator S$S$ or K$K$ is not followed by a sufficient number of arguments, the evaluation can not be done. The argument is always the shortest non-empty string that follows the operator and is a valid expression. In the expression SK(SK)SK$SK(SK)SK$ the (three) arguments of the first S$S$ operator are: K$K$, (SK)$(SK)$ and S$S$. The last symbol (i.e. K$K$) is not an argument of the S$S$ operator, since S$S$ only takes 3$3$ arguments.

Write a program that will read in an expression and reduce it. Your program should output the current expression after each single evaluation. If there are many possible operators that can be evaluated, you should always choose the the leftmost one. It is guaranteed that this procedure ends in a finite number of steps and that total size of the output will not exceed 20\,000$20\,000$ characters.

## Input

The first line contains a string that is composed of letters S$S$ and K$K$ and validly-nested parentheses. The string will not contain unnecessary parentheses.

### Input limits

The length of the string will not exceed 1000$1000$.

## Output

Output all the steps in the procedure of reduction. The expressions in the output should not contain redundant parentheses. For the exact format see the examples below.

### Output limits

The total size of the output will not exceed 20\,000$20\,000$ characters.

## Examples

### Input

SKKK

### Output

SKKK
=KK(KK)
=K

### Input

S(SK)(KS)(SKK)

### Output

S(SK)(KS)(SKK)
=SK(SKK)(KS(SKK))
=K(KS(SKK))(SKK(KS(SKK)))
=KS(SKK)
=S

### Input

SK

### Output

SK

### Input

S(KSK)

### Output

S(KSK)
=SS

### Input

S(KK)S

### Output

S(KK)S

### Comment

In the first example, we first evaluate the leftmost S$S$ operator, and then the leftmost K$K$ operator.

In the second example, we evaluate the leftmost operator in each step.

In the third example, none of the operators has a sufficient number of arguments, therefore the expression can not be reduced.

In the fourth example, the leftmost operator (namely, the S$S$ operator) has an insufficient number of arguments, therefore we evaluate the leftmost K$K$ operator.

In the fifth example, none of the operators has a sufficient number of arguments, therefore nothing can be reduced. The leftmost K$K$ operator only has one arguments, since its "scope" is only within the parentheses.