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

Assembly line

Assembly line

The last worker in a production line at the factory of Automated Composed Machinery is worried. She knows that her job hangs in the balance unless her productivity increases. Her work consists of assembling a set of pieces in a given sequence, but the time spent on assembling pieces a and b and then c may not the same as that on assembling pieces b and c, and then assembling a with the resulting component. Only two consecutive pieces may be assembled at a time, and once they are assembled they behave as another piece in terms of the time needed for further assembly. In order to aid her, you need to find the optimal way to assemble all components. The input to your program will be a set of symbols representing (types of) pieces, and a so-called assembly table representing the time it takes to assemble them, as well as the type of the resulting component. For instance, we may have two symbols \{a, b\}, and the following table: This means, for example, that two pieces of type a and a may be assembled in 3 minutes, and the result is a component of type b, in that the time required to assemble it again with another piece of, say, type a is 6 minutes, and so on. Note that the table is not symmetric, i.e. assembling b and a may be more time-consuming than a and b. For a sequence of components labelled aba, the two possible solutions are: \begin{itemize} \item (ab)a = ba = a with time time(ab) + time(ba) = 5 + 6 = 11. \item a(ba) = aa = b with time time(ba) + time(aa) = 6 + 3 = 9. \end{itemize} So the result for this case would be a piece of type b in 9 minutes (denoted 9\texttt{-}b). \InputFile The input consists of several test cases. Each test case begins with a line containing a natural number \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{26}), followed by a line with \textbf{k} symbols (characters in \[\textbf{a}-\textbf{z}\]) separated by spaces. The following \textbf{k} lines contain the assembly table: the \textbf{i}-th line has kpairs of the form time\texttt{-} result, where time is an integer between \textbf{0} and \textbf{1 000 000} inclusive, and result a symbol belonging to the preceding set. The \textbf{j}-th pair in the \textbf{i}-th line represents the time to compose pieces of types represented by the \textbf{i}-th and \textbf{j}-th symbols, along with the type of the resulting piece. After the table, a line with an integer n indicates the number of lines that follow, each line being a string of at most \textbf{200} symbols. Each of these lines is a sequence of components that need to be assembled together in the right order. The input will finish with a line containing '\textbf{0}', which should not be processed. \OutputFile For each test case, print \textbf{n} lines, each with an integer time and a symbol result in the format time\texttt{-} result. Each line represents the minimum time and the type of the resulting piece for the corresponding case in the input. In case of a tie among several possible results with the same minimum time, choose from among those the piece whose type letter appears first in the line that contained the ksymbols at the beginning of the test case. (For example, if that line was \textbf{a c b} and both \textbf{c} and \textbf{b} can be obtained with minimum \textbf{cost5}, print \textbf{5}\texttt{-}\textbf{c}). There must be an empty line between the output of different test cases.
Ліміт часу 10 секунд
Ліміт використання пам'яті 256 MiB
Вхідні дані #1
2
a b
3-b 5-b
6-a 2-b
2
aba
bba
2
m e
5-e 4-m
3-e 4-m
1
eme
0
Вихідні дані #1
9-b
8-a

7-m