Message
Time limit1sMemory limit128 MB
Given error and succession probabilities for a Martian alphabet, find the most likely original word for each intercepted message using maximum likelihood.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Probability
- Solved
- No attempts yet
Problem
When little Sascha grew up, she lost the habit of pronouncing words in whatever way was easiest for her instead of the way they are correctly pronounced. She never lost her linguistic creativity, though. When the Earth Allied Forces (EAF) discovered that she also had brilliant mathematical insight and a talent for puzzles and secret messages, she was immediately made Head of the EAF Intelligence Department.
Sascha's current task is to interpret intercepted internal messages of the hostile Mars Federation. Although every Martian message consists of just a single word, the job is hard, because two factors shape the message that is actually intercepted:
- The extraterrestrial environment is so harsh that transmission errors occur. An error replaces a character with another character of the same Martian alphabet, so the intercepted text can look quite different from what was sent.
- Linguistic structure matters. In Martian there are relations between two consecutive characters: some characters are more likely to precede a given character than others (the same happens in English, where an 'h' is more likely to have been preceded by a 't' than by a 'q').
Fortunately, for every pair of alphabet characters we know the probability that a received character was actually sent as the true character , and the probability that a character appears in a clean Martian word given that its immediate predecessor was .
Given all these probabilities, Sascha wants the maximum-likelihood text of a received message: the most likely word the Martians originally sent. Write a program that computes it for several intercepted messages across several Martian dialects.
Formally, for an intercepted message and a candidate original message of the same length, the likelihood is
where is the probability that a received character was originally , and is the probability that character immediately follows character . You must output the that maximizes this likelihood.
As a small example, take a local alphabet with only the characters 'a' and 'b', with the receiving-error probabilities and character-succession probabilities shown below.
Receiving error probabilities (row = true character , column = received character ; each entry is the probability that a received was originally sent as ):
Character succession probabilities (row = previous character , column = current character ; each entry is the probability that the current character has the row character as its immediate predecessor):
If the intercepted message is just 'a', it could originally have been either 'a' or 'b'. With no previous character, only the error probabilities matter, and the maximum-likelihood message turns out to be 'a', with probability 0.9.
To extend the example, if the intercepted message is 'ab' we also need the succession probabilities. The probability that the original message was 'aa' is the product of three factors: that the received 'a' was originally 'a' (), that the received 'b' was originally 'a' (), and that 'a' follows a previous 'a' (), giving . Similarly, 'bb', 'ab', and 'ba' have probabilities , , and . The largest is 'bb', so the maximum-likelihood message is 'bb'.
In every case you are asked about, the maximum-likelihood message is unique.
Input
The first line contains an integer , the number of test cases.
Each test case has the following format:
- An integer (), the number of characters in the local Martian alphabet.
- A line with the distinct alphabet characters , separated by single spaces. Alphabet characters are never whitespace.
- lines of receiving-error probabilities, in the order the characters were listed. Line corresponds to the true character and contains floating-point numbers separated by single spaces. The value () is the probability that an observed character was originally sent as (so for every ).
- lines of character-succession probabilities, in the same order. Line corresponds to the case where is the immediate predecessor and contains floating-point numbers separated by single spaces. The value is the probability that a character has as its immediate predecessor (so for every ).
- An integer (), the number of intercepted messages in this alphabet.
- lines, each an intercepted message. Every message is non-empty, case-sensitive, at most 300 characters long, and consists only of characters from the local alphabet.
No floating-point number has more than 10 digits after the decimal point.
Output
For each intercepted message, in every test case, output a single line containing the maximum-likelihood original Martian message for that intercepted message.