Evolution
Time limit1sMemory limit128 MB
Given N DNA strings linked in an unknown parent-child order, compute each creature's probability of being the original ancestor.
- Level
Medium7 of 10
- Topics
- Probability, Bit manipulation, Dynamic programming
- Solved
- No attempts yet
Problem
Dr. Beverly studies an unusual organism whose DNA is a single string of letters drawn from an alphabet of size . One hour after it is born, an individual produces exactly one offspring, and then lives on for a short while longer. This repeats generation after generation, forming a single line of descent.
When an offspring is produced, its DNA is copied from its parent, but each of the letters may mutate independently. With probability a given letter mutates: it is replaced by a letter chosen uniformly at random from the size- alphabet (which may, by chance, be the same letter). With probability the letter is left unchanged. Consequently, for one parent-to-child step, a fixed letter stays equal to a given target letter with probability , and becomes a specific different letter with probability ; the transition probability of a whole string is the product over its positions.
Dr. Beverly began an experiment with a single individual, then got distracted and forgot about it. Returning later, she found the remains of creatures — the original individual together with its unbroken chain of descendants — but she no longer knows their birth order. She sampled all DNA strings. For each sampled creature, compute the probability that it was the original individual with which the experiment began.
Assume that, a priori, each of the creatures is equally likely to have been the original, and that the strings are presented in a random order.
Input
The first line contains a single integer , the number of test cases. Each test case has the following format:
- One line with three integers , , and one real number , separated by single spaces, where , , , and .
- following lines, each containing a string of length over a fixed alphabet of size (a subset of the uppercase letters A–Z, the same alphabet for all strings). The string on the -th line is the DNA of the -th creature.
Output
For each test case, output lines. The -th line contains the probability that the -th creature given in the input was the original individual, rounded to exactly six digits after the decimal point.