Typing monkey
Time limit1sMemory limit256 MB
Given per-letter probabilities and two words P and Q, compute the probability that P appears as a substring before Q does.
- Level
Medium7 of 10
- Topics
- Probability, String matching, Matrix
- Solved
- No attempts yet
Problem
You have a monkey that can work a typewriter. It types lowercase English letters one at a time, without stopping, drawing each letter from a fixed probability distribution, and the keypresses are independent of one another.
You believe the monkey will eventually type out the complete works of Shakespeare. Your friend believes it is more likely to write another novel in the Harry Potter series. To settle the argument, shrink each work down to a single word and compute the probability that one word turns up before the other.
A word is produced the moment it appears as a substring of the letters the monkey has typed so far. Given two words and , compute the probability that the monkey produces before .
Input
The first line contains the number of test cases .
Each test case takes two lines. The first line contains the probabilities of the monkey typing each letter from a to z, in that order, separated by single spaces. The second line contains the two strings and , separated by one space. Both strings consist of lowercase English letters only.
- and
- and are different.
- Every letter that occurs in or in has probability greater than zero.
- No input makes the monkey complete and at the same moment.
Output
For each test case, print on its own line the probability that the monkey produces before . Round the value at the seventh digit after the decimal point and always print exactly six digits after the decimal point.