eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Synnerg Lifeform

Synnerg Lifeform

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. \begin{itemize} \item There are more than one types of synnerg. \item Any new born synnerg have the same lifetime. \item Two synnergs (of certain types) can extend their lifetimes by unifying themselves together which will also transform them into a new target synnerg. The lifetime of this new target is the summation of both sources' lifetimes multiplied with an amplification factor. The type of this new target may vary from its sources. From many experiments, scientists found a set of rules to describe the unification of each synnerg pair . Some of these rules can be shown as the following table. \end{itemize} \begin{itemize} \item When scientists arrange two or more of new born synnergs into a sequence, each synnerg in this sequence will try to unify itself with the one on its left or its right (and make the sequence shorter). This unification process will continue again and again (recursively). There might be lots of possible ways to unify an input sequence which also affect the lifetime of each unified synnerg. \end{itemize} For example of unification steps, please see the following table. In the first example, at the first step there is a sequence of \textbf{4} new born synnergs "\textbf{a a b b}" where their lifetimes are "\textbf{1 1 1 1}" respectively. In the second step, these synnergs unify themselves into a sequence of two synnergs "\textbf{aa\[a a\] bb\[b b\]}" (using rule no.\textbf{1} and \textbf{3}) where aa's lifetime is \textbf{2=(1+1)*1} and bb's lifetime is \textbf{6=(1+1)*3}. In the third step, "\textbf{aa bb}" unify themselves into "\textbf{x\[aa bb\]}" (using rule no. \textbf{4}) where its lifetime is \textbf{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 "\textbf{AA\[a a\] bb\[b b\]}" (using rule no.\textbf{2} and \textbf{3} instead) where their lifetimes are\textbf{34=(1+1)*17} and \textbf{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 "\textbf{a c a c}". Please be notify that each rule is not sensitive to the order of its sources. \textbf{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. \InputFile Input is a standard input which contains \textbf{2} parts of data which are separated by a blank line. The first part is a set of unification rules. \begin{itemize} \item Each line in this first part contains one of unification rule. \item Each rule consists of \textbf{4} fields separated by spaces. \item The first \textbf{3} fields are target, source \textbf{#1} and source \textbf{#2} respectively. Each fields is a synnerg name which is represented by a string of at most \textbf{20} alpha-numeric characters. \item The last field is the amplification factor which is a positive integer less than or equal to \textbf{100}. \end{itemize} The second part is a set of input sequences of synnergs. \begin{itemize} \item Each line in this second part contains one input sequence of synnergs. \item Each input sequence is a sequence of synnergs separated by spaces. \end{itemize} The blank line after the second part is the termination of the input. \OutputFile For each sequence of input, write \textbf{2} parts of output as follows \begin{itemize} \item In the first line, write the total of number of highest synnergs followed by a space and then the maximum lifetime of these synnergs. \item In the following lines, write each of the solutions in each line. Each solution contains \textbf{3} fields separated by spaces. These fields are a synnerg, its starting and finishing offset. If there are two or more solutions, they must be sorted by ascending order. The priority of comparison in sorting is the \textbf{2}^nd field, the \textbf{3}^rd field and the \textbf{1}^st field (or the starting offset, the finishing offset and the synnerg). For comparisons in sorting, the \textbf{2}^nd and \textbf{3}^rd field comparison is based on numerical values, and the \textbf{1}^st field is based on alphabetical/lexicographical (ASCII) order. \end{itemize} \textbf{More Explanations} There are \textbf{6} rules and \textbf{5} input sequences in this sample input. For the first case, even though there is a completed unification "\textbf{x\[aa\[a a\] bb\[b b\]\]}" (starting from \textbf{1} to \textbf{4}), its lifetime is only \textbf{16 (= \{(1+1)*1 + (1+1)*3\}*2)} times of a newborn, which is less than \textbf{34} of "\textbf{AA\[a a\]}" starting from \textbf{1 }to \textbf{2}. For the second case, there are \textbf{2} solutions. The first is "\textbf{AA}" starting from \textbf{1} to \textbf{2}. The second is "\textbf{x}" starting from \textbf{1 }to \textbf{5}. For the third case, there is only one solution. "\textbf{x}" now is the only winner (with \textbf{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. ("\textbf{c c a 3}" equals "\textbf{c a c 3}".)
Ліміт часу 1 секунда
Ліміт використання пам'яті 256 MiB
Вхідні дані #1
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
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