## TLE '17 Contest 4 P2 - Microwave Buttons

View as PDF

Points: 5 (partial)
Time limit: 2.0s
Memory limit: 256M

Authors:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
The return of the microwave.

Fax McClad, Croneria's hungriest bounty hunter, wants to cook a quick meal in his microwave. Unfortunately, some of the buttons on his microwave have broken down.

Fax wants to microwave his food for hh:mm:ss, where hh is a 2 digit number representing the number of hours, mm is a 2 digit number representing minutes, and ss is a 2 digit number representing seconds. Note that mm and ss do not necessarily have to be less than .

For reference, there are seconds in a minute, and seconds in an hour.

The list of usable buttons will be given in a string as input. There will be at least one usable button.

Can you tell Fax the closest time that can be inputted to his desired time? That is, tell him the time with the smallest time difference that can be inputted in the microwave. Note that all digits (including zeroes) of the inputted time must be present in the string of working buttons.

#### Input Specification

The first line of input will contain Fax's desired time in the form hh:mm:ss.

The second line of input will contain a string of at least character and up to characters, consisting of unique digits through in order. A digit in the string means that the corresponding button is working.

#### Output Specification

Output the closest time in hh:mm:ss format that can be inputted in the microwave to Fax's desired time.

If there is more than one answer, output any of them.

#### Sample Input 1

01:17:27
012345689

#### Sample Output 1

01:16:86

#### Explanation for Sample Output 1

Fax wants to microwave his food for seconds. All of the buttons on the microwave work, except for 7.

A second difference is the best difference possible. For example, all of the digits in 01:16:86 are allowed, and this time results in seconds. Another possible answer is 01:16:88.

#### Sample Input 2

57:41:26
03578

#### Sample Output 2

57:38:88