Problem E
Easy Probability
As a Hunter, you will undoubtedly face difficult obstacles during your journey. At such time, a Hunter is expected to model the situation using Mathematical models, and apply probability and statistics knowledge to overcome the situation.
Thus, the third round of the Hunter Exam is based on the following game with coins:
-
Before the game starts, Gon selects a string
and Killua selects a string . The two strings must contain only characters ‘H’ and ‘T’. -
A game master will flip a coin an infinite number of times. After each flip, the result (either ‘H’ or ‘T’ — representing head or tail) is appended into a string
. The string is empty at the beginning of the game. -
After some coin flip:
-
If both
and become a substring of , the game ends in a draw. -
If only
becomes a substring of , Gon wins, and the game ends. -
If only
becomes a substring of , Killua wins, and the game ends.
-
-
Gon and Killua only have finite amount of time. They will stop the game in a draw after
turns.
Please calculate the probability that Gon wins.
Input
The input contains exactly three lines:
-
The first line contains the string
. -
The second line contains the string
. -
The third line contains a real number
with exactly one digit after the decimal point — the probability that a coin flip will result in head .
The length of
Output
The output must contain a single number — the probability that Gon wins.
Your answer will be considered correct if its relative or
absolute error doesn’t exceed
Namely: let’s assume that your answer is
Sample Input 1 | Sample Output 1 |
---|---|
H T 0.5 |
0.5 |
Sample Input 2 | Sample Output 2 |
---|---|
HH TH 0.5 |
0.25 |