In a science laboratory, scientists found a new kind of tiny lifeforms and named them
“synnergs”. Until now, few knowledge about synnergs are known. Some of these knowledge are
as follow.
In the first example, at the first step there is a sequence of 4 new born synnergs “a a b b” where
their lifetimes are “1 1 1 1” respectively. In the second step, these synnergs unify themselves into
a sequence of two synnergs “aa[a a] bb[b b]” (using rule no.1 and 3) where aa's lifetime is
2=(1+1)*1 and bb's lifetime is 6=(1+1)*3. In the third step, “aa bb” unify themselves into “xaa
bb where its lifetime is 16=(6+2)*2.
In the second example, at the first step, the beginning sequence is the same sequence in the first
example. But they alternatively unify themselves into “AA[a a] bb[b b]” (using rule no.2 and 3
instead) where their lifetimes are 34=(1+1)*17 and 6(1+1)*3 respectively. This sequence is also
the final sequence which means that it is unable to unify furthermore but it is not a completely
unified sequence. However, this alternative final sequence has longer life time comparing to the
final sequence in the first example.
The third and fourth example also show the alternative ways of unifying the sequence “a c a c”.
Please be notify that each rule is not sensitive to the order of its sources.
Goal
The scientists would like you to create a program that can find all highest lifetime synnergs that
can be created by unifying a given sequence of newborn synnergs. The solution may not always
be the one that is completely unified. Only part or sub-sequence unification is also acceptable.
Input is a standard input which contains 2 parts of data which are separated by a blank line.
The first part is a set of unification rules.
The second part is a set of input sequences of synnergs.
The blank line after the second part is the termination of the input.
For each sequence of input, write 2 parts of output as follows
aa a a 1 AA a a 17 bb b b 3 x aa bb 2 x x a 2 c c a 3 a a b b a a b b a a a b b a a a c a c a c
1 34 AA 1 2 2 34 AA 1 2 x 1 5 1 70 x 1 6 1 6 c 1 2 1 21 c 1 3
There are 6 rules and 5 input sequences in this sample input.
For the first case, even though there is a completed unification “x[aa[a a] bb[b b]]” (starting from 1
to 4), its lifetime is only 16 (= {(1+1)*1 + (1+1)*3}*2) times of a newborn, which is less than 34 of
“AA[a a]” starting from 1 to 2.
For the second case, there are 2 solutions. The first is “AA” starting from 1 to 2. The second is “x”
starting from 1 to 5.
For the third case, there is only one solution. “x” now is the only winner (with 70 points).
For the fourth and the fifth cases, (parts of) the results has been unified according to the last rule.
Since each rule is not sensitive to the order of its sources, any rule is applicable if both of its source
are found. (“c c a 3” equals “c a c 3”.)
Migrated from old NTUJ.
2009Phuket
No. | Testdata Range | Score |
---|