| Udmurt SU Contest 2010 |
|---|
| Finished |
Combinatory logic is a field in mathematical logic developed in the first half of the $$$XX$$$ century by Moses Sheinfinkel and Haskell Curry. Main elements of combinatory logic are constants, variables and terms. To construct terms we use the following induction rules:
The expression $$$(AB)$$$ means the operation of applying function (combinator) $$$A$$$ to its argument $$$B$$$. To shorten the notations, the expression may have omitted brackets, if so they can be recovering by applying associativity rule:
$$$$$$ABC = ((AB)C).$$$$$$
So according the rule above, let's assume that the expression $$$ABC$$$ is also the correct form of combinatory term.
Let's note variables as lowercase letters and combinators as uppercase letters. In the problem only the combinators can be constants.
Now let's define the simple combinators $$$I$$$, $$$K$$$, and $$$S$$$:
If we don't go deep into details of combinatory logic, we may compute the combinatory terms only using the following simple rules:
$$$Ia = a$$$;
$$$Kab = a$$$;
$$$Sabc = ac(bc)$$$.
Let's consider some more examples:
$$$$$$S(KII)ab = (KII)b(ab) = (I)b(ab) = Ib(ab) = b(ab);$$$$$$
$$$$$$SIISa = IS(IS)a = S(IS)a = S(S)a = SSa;$$$$$$
$$$$$$ \begin {array} {cc} S(KK)I(Kab)b = (KK)(Kab)(I(Kab))b = KK(Kab)(I(Kab))b = \\ = K(I(Kab))b = (I(Kab)) = I(Kab) = (Kab) = (a). \end{array} $$$$$$
Let's assume there was a combinator $$$P$$$ that was applied to the sequence of first $$$n$$$ lowercase Latin letters and the result of the application is the sequence $$$X$$$. Given the number $$$n$$$ and the sequence $$$X$$$, find any expression $$$P$$$ made by primitive combinators $$$I$$$, $$$K$$$, $$$S$$$ and brackets.
The first line contains one integer number $$$n$$$ ($$$1 \leq n \leq 8$$$). The second line contains a nonempty sequence up to $$$8$$$ symbols. It can contain only the first $$$n$$$ lowercase Latin letters.
Output the string with no more than $$$400\, 000$$$ symbols "I", "K", "S", "(" and ")" describing combinatory term $$$P$$$. The maximum nesting level of brackets must not exceed $$$150$$$ and the number of combinators in computation must not exceed $$$150\,000$$$. It is guaranteed that for every input as least one answer exists.
1 a
I
2 a
K
1 aa
SII
1 aaa
S(SII)I
3 acb
S((S(KS)K)(S(KS)K)S)(KK)
2 abb
SS(K(SKK))
| Name |
|---|


