Mixing Colours
Time limit5sMemory limit128 MB
The player picks one colour per token and merges adjacent tokens under the rules to maximize the product of picked certainties, breaking ties in ASCII order.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Probability
- Solved
- No attempts yet
Problem
Frank lives in London and he likes games and maths. Lately he has been playing a simple game on his phone. A row of coloured tokens is given, and on every turn the player merges a pair of adjacent tokens into one token of a certain colour. The turns repeat until a single token is left.
Not every pair of colours can be merged. A set of rules says which combinations are allowed. With the rules
- Blue + Yellow → Green
- Yellow + Red → Orange
- Blue + Orange → Brown
and the row (Blue, Yellow, Red), the game can end with a Brown token after two turns. (Blue, Yellow, Red) → (Blue, Orange) → (Brown)
A rule ignores the order of its two colours. If the rule exists, two adjacent tokens merge into one token whether they sit as or as .
Frank is now in Valencia for a programming contest. He is waiting for the tram to the university and playing this game to fill time, but the sun shines so bright that he cannot see the screen properly. He has only some certainty about the colour of each token, so he wonders which colour is left at the end of the most likely play according to his estimate. Given his estimate of two colours and and the rule , the certainty of the obtained colour is .
One play picks a single colour for every token and merges the whole row into one token under the rules. The certainty of that play is the product of the certainties of the picked colours.
Input
The first line contains , the number of rules describing the allowed combinations of colours. ()
Each of the following lines contains three strings , , representing the rule .
The next line contains the number of test cases .
The first line of each test case contains the length of the row of tokens. ()
Each of the next lines describes one token. It lists pairs of a colour and its certainty , and ends with the word END. ()
The certainties of one token always sum to . Every colour in a test case has appeared first in the rules.
Output
Print one line for each test case.
If some choice of one colour per token merges the whole row into a single token under the rules, print the colour left at the end of the play whose product of picked certainties is largest. When several colours reach that largest certainty, print the one that comes first in lexicographic (ASCII) order.
For there is nothing to merge, so the answer is the colour with the largest certainty in that token.
If no play finishes the game, print GAMEOVER.
Hint
The first case of the first example has only two tokens. Frank is sure that the second token is Yellow, but the first token could be either Red or Orange. The game can be finished in two ways.
- (Red, Yellow) → (Orange), certainty . The rule Yellow + Red → Orange applies with its two colours swapped.
- (Orange, Yellow) → (Yellow), certainty .
The first play is more likely, so the final colour is Orange.
The second case has more tokens and estimates. Two possible plays are
- (Blue, Yellow, Yellow, Red) → (Blue, Yellow, Orange) → (Blue, Yellow) → (Green), certainty
- (Green, Red, White, Black) → (Green, Pink, Black) → (Green, Red) → (Yellow), certainty
The second play is more likely, so the answer is Yellow.
In the third case Frank is sure that the tokens are Blue and Orange. No rule merges those two colours, so the game cannot be finished.
The first case of the second example ends in two plays of equal certainty. In (Red, Red, Yellow), merging the first pair gives Red + Red → Blue and then Blue + Yellow → Green, which leaves Green. Merging the second pair gives Red + Yellow → Purple and then Red + Purple → Cyan, which leaves Cyan. Both plays have certainty , so the answer is Cyan, the colour that comes first in lexicographic order.