#P7062. [NWRRC2014] Combinator Expression

[NWRRC2014] Combinator Expression

题目描述

Combinatory logic may be thought as one of computational models allowing to express any computable function as a composition of functions from a small finite basis. In this problem we consider a restricted variant of BCKW basis, BCKI.

Combinator expression in BCKI basis is a string, corresponding to the following grammar:

⟨Expression⟩ ::= ⟨Expression⟩ ⟨Term⟩ | ⟨Term⟩
⟨Term⟩ ::= ‘(’⟨Expression⟩‘)’ | ‘B’ | ‘C’ | ‘K’ | ‘I’

As we can see from the grammar, the expression is a tree of applications where leafs are combinators B,C,KB, C, K and II. The application is left-associative. For example BICBIC is equivalent to (BI)C,(BI)C, but not to B(IC)B(IC).

For the sake of the explanation we will use lowercase English letters (az)(a \cdots z) to represent sub-expressions. These lowercase letters will not appear in real data. For example, BICBIC can be represented by BxCBxC (that is, BIxC)B\underbrace { I }_{ x }C), x(BICx)x(\underbrace {BIC}_{ x }), xy(BIxCy)xy(\underbrace {BI}_{ x } \underbrace { C }_{ y }), $Bxy (B\underbrace { I }_{ x }\underbrace { C }_{ y })$, but not by BxBx.

We say that in expression pqpq we apply pp to qq. We can employ our intuition by saying that pp is a function and qq is its parameter. However, the evaluation process is quite different from traditional computation — instead of passing values over fixed expression tree, we evaluate by altering that tree so that the result is also some combinator expression.

To evaluate an expression, we need to select some sub-expression, corresponding to one of the patterns specified in the table below — that is, there should exist such xx (and maybe yy and zz) that the pattern from the table becomes equal to the sub-expression. Then we need to replace the sub-expression with the reduction result from the table.

Pattern Reduction result Description
BxyzBxyz x(yz)x(yz) Composition function (Zusammensetzungsfunktion)
CxyzCxyz (xz)y(xz)y Exchange function (Vertauschungsfunktion)
KxyKxy xx Constant function (Konstanzfunktion)
IxIx Identity function (Identitätsfunktion)

After the replacement took place we must repeat the process, until there remains no suitable subexpressions. This final expression is normal form of the original one. For example, in expression CIC(CB)ICIC(CB)I we can make the following letter assignment

$$\underbrace { C }_{ C }\underbrace { I }_{ x }\underbrace { C }_{ y }\underbrace {(CB)}_{ z }I $$

and see that CIC(CB)I(((CI)C)(CB))I(((Cx)y)z)ICIC(CB)I ≡ (((CI)C)(CB))I ≡ (((Cx)y)z)I contains CC combinator pattern and thus reduces to ((xz)y)II(CB)CI:((xz)y)I ≡ I(CB)CI:

$$(\underbrace { C }_{ C }\underbrace { I }_{ x }\underbrace { C }_{ y }\underbrace { (CB) }_{ Z })I \rightarrow (\underbrace { I }_{ x } \underbrace {(CB)}_{ z }\underbrace { C }_{ y })I $$

One more example: B((CK)I)ICB((CK)I)IC expression. Let us first reduce combinator B:B:

$$(\underbrace { B }_{ B }\underbrace { ((CK)I) }_{ x }\underbrace { I }_{ y }\underbrace { C }_{ z } \rightarrow \underbrace { ((CK)I) }_{ x } (\underbrace { I }_{ y }\underbrace { C }_{ z }) $$

Now, let's reduce the last I:I:

$$((CK)I)(\underbrace { I }_{ I } \underbrace { C }_{ x }) \rightarrow ((CK)I)C $$

And now we finish evaluation with two more reductions:

$$((\underbrace { C }_{ C }\underbrace { K }_{ x }) \underbrace { I }_{ y }) \underbrace { C }_{ z } \rightarrow (\underbrace { K }_{ K }\underbrace { C }_{ x }) \underbrace { I }_{ y } \rightarrow C $$

It is possible to show that the normal form remains the same irrespectable to the order of evaluation. For example, the following evaluation order:

$$C(K(II)(\underbrace { I }_{ I }\underbrace { C }_{ x })) \rightarrow C(K(\underbrace { I }_{ I }\underbrace { I}_{ x })(C)) \rightarrow C((\underbrace { K }_{ K }\underbrace { I }_{ x }) \underbrace { C }_{ y }) \rightarrow CI $$

leads to the same result as

$$C(K(\underbrace {I}_{ I }\underbrace { I }_{ x })(IC)) \rightarrow C((\underbrace { K }_{ K }\underbrace { I }_{ x })\underbrace { (IC)}_{ y }) \rightarrow CI $$

However, as you see, the number of reductions is different: 33 in the first case and 22 in the second. This poses an interesting problem -- to find an evaluation order with the minimal number of reductions for a given formula.

Your task is to write a program which finds the minimal number of reductions required for a given combinator expression to be evaluated to its normal form.

输入格式

The only line of the input file contains a combinator expression corresponding to the grammar above. The length of the expression does not exceed 3000030 000 . The expression contains no whitespaces or symbols not specified in the grammar.

输出格式

Output a single integer — the minimal number of reductions required for the given formula to evaluate it to normal form.

C(K(II)(IC))

2

CIBI

3

BBBBBCCCCCKKKKKIIIII

15

提示

Time limit: 1 s, Memory limit: 256 MB.