A relay-race ledger records, for each of the n legs of a long-distance relay, the name of the runner holding the baton at the end of that leg. A scoring-booth malfunction left some legs unrecorded; those legs are written using the reserved placeholder token MISSING. Officials know the baton is never reset mid-race, so whenever a leg's holder is missing, the true holder for that leg is the same runner who held the baton at the closest earlier leg whose holder was actually recorded. The very first leg is always guaranteed to have been recorded. Reconstruct the complete ledger.
Line 1: an integer n — the number of legs.
Line 2: n space-separated tokens. Each token is either a runner's name (1 to 20 characters, using only uppercase letters, lowercase letters, and digits, and never equal to the literal text MISSING) or the placeholder token MISSING. The first token is guaranteed not to be MISSING.
n space-separated tokens: the reconstructed ledger, where every MISSING has been replaced by the name of the runner holding the baton at the nearest earlier recorded leg.
1 <= n <= 100000.
Example 1
Input
6 Alex MISSING MISSING Bri MISSING Cee
Expected
Alex Alex Alex Bri Bri Cee
Explanation
Leg 1 is recorded as Alex. Legs 2 and 3 are MISSING, so both are filled with Alex, the nearest earlier recorded holder. Leg 4 is recorded as Bri. Leg 5 is MISSING, so it is filled with Bri. Leg 6 is recorded as Cee. The reconstructed ledger is "Alex Alex Alex Bri Bri Cee".
Example 2
Input
1 Solo
Expected
Solo
Explanation
There is only one leg and it is already recorded as Solo, so no filling is needed and the output is simply "Solo".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →