Facebook Hacker Cup '14 Final Round P3 - Fortunate Wheels
View as PDFFacebook Hacker Cup 2014 Finals
Kit is competing on the latest popular gameshow, Fortunate Wheels. On
this show, there is a secret word  
consisting only of uppercase letters, known only to the host, and
contestants can pay points to buy sequences of letters in hopes of
matching part of 
 and earning more points! This show is clearly a
scam, as the probability of earning more points than are spent is
extremely low. Fortunately, Kit has come prepared – he knows the secret
word! Even so, getting as many points as possible will not be easy.
There are  
 basic deals which contestants can take.
The 
-th deal costs 
 points 
, and
allows any sequence of 
 letters 
 to be
purchased. Furthermore, deals can be combined to purchase longer
sequences! Combining a deal with cost 
 and length 
 with
another deal (potentially the same one) with cost 
 and length
 creates a new deal with cost 
 
and length 
 (as long as 
),
which can in turn be used to create even bigger deals. For
example, if 
, then a basic deal with cost and length equal to 
could be combined with itself repeatedly to yield a new deal with both
cost and length equal to any positive integer smaller than 
.
Once Kit purchases a sequence of letters using one (potentially
non-basic) deal, it will be matched against the secret word – twice! The
host will spin the First Fortunate Wheel to select the starting index in
 for the first matching, which is chosen uniformly at random such
that the sequence will fit entirely within 
. Then, the host will spin
the Second Fortunate Wheel to select the starting index for the second
matching, which is chosen uniformly at random such that the sequence
will fit entirely within 
 and such that the value given by the
First Fortunate Wheel will not be repeated. For example, if the sequence
consists of a single letter, the First Fortunate Wheel might yield any
of the indices in 
 with probability 
each, and then the Second Fortunate Wheel might yield any of the
remaining indices with probability 
each. On the other hand, if the sequence has length 
, then the
first Wheel might yield either 
 or 
, and the second Wheel must yield
the other. If, for both generated indices, the sequence miraculously
happens to be equal to the substring of 
 of the same length starting
at that index, then Kit will earn back 
points 
, where 
 is the
length of the sequence. If even one letter is off in either matching,
however, Kit will earn no points at all! What has the game show industry
come to?
Kit is carefully considering his first turn of the game. He obviously
wants to maximize the number of points he'll gain, but worries that
choosing the very best move might be suspicious. As such, he'd like to
find the expected point values of the  
 best
distinct moves before making his decision. Two moves are distinct if
they involve purchasing different sequences of letters – the deal(s)
used are ignored. Note that moves can have negative expected point
values, due to the costs of deals.
Input Specification
Line : 
 integer, 
 
, the number of test cases.
For each test case:
Line : 
 integers, 
, 
, 
, 
, 
, and 
.
Line : 
 string, 
.
Next  lines: 
 integers, 
 and 
, for 
.
Output Specification
For each test case, output  real numbers on one line (accurate to
within 
 in absolute or relative value), the 
 largest
expected point values which can be earned, in descending order.
Sample Input
2
1 3 0 1 5 6
ZZ
2 1
3 4 1 4 3 2
FOXENINBOXEN
5 2
1000 11
2 2
Sample Output
24.00000 -2.00000 -2.00000
7.05556 3.49091 3.49091 3.49091
Explanation of Sample
In the first test case, Kit's best move is to use the basic deal,
costing  points, to purchase the sequence of letters 
Z. No matter
what pair of indices the two Fortunate Wheels yield, this sequence will
match and earn Kit  points. Any
other sequence shorter than 
 cannot match 
 at even a single index,
so Kit's second- and third-best moves consist of using the basic deal to
purchase any other single-letter sequence, and simply losing the 
points.
In the second test case, Kit's best move consists of combining the third
basic deal with itself to yield a deal with cost 5 and length 4, and
then purchasing the sequence OXEN. His three next-best moves, which
are the only other moves which get him a positive expected point value,
involve using the third basic deal to purchase the sequences OX,
XE, and EN.
Comments