eolymp
bolt
Try our new interface for solving problems
Problems

Mixing Colours

Mixing Colours

Frank lives in London and he really likes games and maths. Lately, he has been playing a game on his mobile phone. It is simple, a sequence of coloured tokens is provided and each turn the player has to combine a pair of adjacent tokens to obtain a new token of certain colour. This process is repeated until the whole sequence results in only one final token. The problem is that not every pair of colours can be merged. There is a set of rules describing the allowed combinations. For example, given the following rules \textbf{blue + yellow → green} \textbf{yellow + red → orange} \textbf{blue + orange → brown} and the sequence (\textbf{blue}, \textbf{yellow}, \textbf{red}), the game could be finished obtaining a brown token after two steps: (\textbf{blue},\textbf{yellow}, \textbf{red}) \textbf{→} (\textbf{blue}, \textbf{orange}) \textbf{→} (\textbf{brown}). Frank is now in Valencia attending a famous programming contest and he is waiting for the tram to go to the university. Meanwhile, he is playing this game to fill time but the sun is shining so bright that he can not properly see the screen of his phone. He has some certainty about the possible colour of each token and he is wondering what will be the resulting colour following the most likely play of the game according to his estimation of the input sequence. Given Frank’s estimation of two colours \textbf{A} and \textbf{B}, and the combination \textbf{A + B → C}, the certainty of the obtained colour \textbf{C} is computed as \textbf{cer(C) = cer(A)·cer(B)}. \InputFile The first line contains the number of rules \textbf{r }(\textbf{0 }< \textbf{r }≤ \textbf{100}) describing the allowed combinations of colours. Each of the following \textbf{r }lines contains three strings \textbf{s_1}, \textbf{s_2}, \textbf{s_3} representing the combination \textbf{s_1 + s_2 → s_3}. Next line shows the number of test cases \textbf{t}. For each test case an integer \textbf{c }(\textbf{0 }< \textbf{c }≤ \textbf{500}) indicates the length of the input sequence of tokens. Each of the next \textbf{c }lines describes a token, it contains a list of pairs (\textbf{k}, \textbf{cer(k)}) providing a colour \textbf{k} and its corresponding certainty (\textbf{0} < \textbf{cer(k)} ≤ \textbf{1.0}). The list ends with the word \textbf{END}. The sum of the certainties for a certain token always is \textbf{1.0}. Every colour in a test case has appeared first in the rules describing the game. \OutputFile For each test case, print the resulting colour of the most likely play of the game. If there is no possible combination to finish the game then print \textbf{GAMEOVER}. \textbf{Explantion of the samples} \textit{\textbf{First case}} In the first case there are only two tokens. Frank is sure that the second token is \textbf{Yellow}, but the first token could be either \textbf{Red} or \textbf{Orange}. There are two possible solutions to the game: 1. \textbf{(Red; Yellow) → (Orange) :} \textbf{cer(Orange) = 0.7} 2. \textbf{(Orange; Yellow) → (Yellow) :} \textbf{cer(Yellow) = 0.3} Hence, according to Frank’s estimation the most likely play is the number \textbf{1} such that the final colour is \textbf{Orange}. \textit{\textbf{Second case}} In the second case there are several tokens and estimations. Two possible solutions are: 1. \textbf{(Blue,Yellow,Yellow,Red) → (Blue,Yellow,Orange) → (Blue,Yellow) → (Green)} \textbf{cer(Green) = 0.006} 2. \textbf{(Green,Red,White,Black) → (Green,Pink,Black) → (Green,Red) → (Yellow)} \textbf{cer(Yellow) = 0.036} Hence, according to Frank’s estimation the most likely play is the number \textbf{2} such that the final colour is \textbf{Yellow}. \textit{\textbf{Third case}} In this final case Frank is sure that there are two tokens \textbf{Blue} and \textbf{Orange}. There is no rule in the defined game that allows the combination of those colours, hence, there is no solution for this sequence of tokens.
Time limit 10 seconds
Memory limit 64 MiB
Input example #1
7
Blue Yellow Green
Yellow Red Orange
Green Red Yellow
White Red Pink
Pink Black Red
Orange Red Red
Yellow Orange Yellow
3
2
Red 0.7 Orange 0.3 END
Yellow 1.0 END
4
Blue 0.6 Green 0.4 END
Red 0.2 Orange 0.6 Yellow 0.2 END
White 0.9 Yellow 0.1 END
Red 0.5 Black 0.5 END
2
Blue 1.0 END
Orange 1.0 END
Output example #1
Orange
Yellow
GAMEOVER
Source 2013 ACM Southwestern Europe Regional Contest (SWERC), November 17