Bus Clock Display

Time limit1sMemory limit128 MB

Summary
Given up to 100 partial 7-segment clock readings with min and max elapsed minutes between consecutive readings, determine the time at each reading or report how many possibilities remain.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, Implementation, Brute force
Solved
No attempts yet

Problem

You are on a long bus trip, and above the driver's seat hangs a digital clock that uses the classic 7-segment display. Because the bus is crowded and passengers carry all sorts of things, your view can be blocked, so some segments of the clock may be hidden.

Even when some segments are hidden, the time can sometimes still be read unambiguously. For example, with several segments obstructed a reading may be compatible with only one time, say 12:04. In other cases several possibilities remain: looking at the last digit alone, for instance, it might be any of 0, 5, 6, 8, or 9, so you cannot be sure.

When that happens you take another look a little later. Meanwhile the passengers have shifted, so a different part of the display is now visible. On its own the new reading may be even more ambiguous, but you know the range of time that elapsed between the two readings, so combining them gives you more information. This lets you determine not only the current time but also, retroactively, the time of the earlier reading.

For one bus you are given several readings of its clock (each reading may have some segments hidden) together with the minimum and maximum number of minutes that elapsed between every two consecutive readings. Considering only (and all of) the times consistent with every reading, compute the time the clock was showing at each reading.

Input

The input contains several bus descriptions.

Each description begins with a positive integer SS, the number of readings taken, with 1≤S≤1001 \le S \le 100. The SS readings then follow in order.

Each reading is given by 28 characters that describe the state of every segment of the four-digit clock. The characters are separated by at least one space or newline, and extra spaces may be used to lay them out nicely.

A single digit consists of the following seven segments. a, g, d are horizontal segments and f, b, e, c are vertical segments:

 a
f b
 g
e c
 d

The 28 characters are obtained by reading the whole four-digit display row by row from the top, each row left to right. In order, they are: the 4 top horizontal segments, the 8 upper vertical segments (for each digit the left one f then the right one b), the 4 middle horizontal segments, the 8 lower vertical segments (for each digit the left one e then the right one c), and the 4 bottom horizontal segments. The colon (:) between hours and minutes is only a visual marker and is not part of the 28 characters.

Each character means:

  • - : a horizontal segment that is lit
  • | : a vertical segment that is lit
  • . : a segment that is off (known to be unlit)
  • ? : a segment that cannot be seen, so its state is unknown

The segments lit by each digit are:

DigitLit segments
0a b c d e f
1b c
2a b d e g
3a b c d g
4b c f g
5a c d f g
6a c d e f g
7a b c
8a b c d e f g
9a b c d f g

The clock uses the 24-hour format and shows values from 0:00 to 23:59. The first of the four digit positions never shows a zero: it is blank (all its segments off) whenever the number of hours is less than 10.

Between every two consecutive readings there are two integers Tmin⁡T_{\min} and Tmax⁡T_{\max}, separated by a space, giving the minimum and maximum number of minutes that may have elapsed between those readings, with 0≤Tmin⁡≤Tmax⁡≤1200 \le T_{\min} \le T_{\max} \le 120.

The input ends with a line containing a single 0.

Output

For each bus description, print one line for every reading taken for that bus.

If the time is uniquely determined, print the hours and minutes separated by a colon. Print the hours without a leading zero (so three or four digits in total) and the minutes always with two digits, e.g. 9:05, 12:04, 23:59.

If several times are possible, print ambiguous, N possibilities, where NN is the number of distinct times that could have been shown at that moment. Count exactly those times (and all of them) that are consistent with every reading of that bus.

Separate the output of two consecutive buses with one blank line.

Examples5

  1. Example 1

    Input
    3
     -   -      .   -
    . | . |    | | | |
     -   -      -   .
    | . . |    . | | |
     -   -      .   -
    16 30
     ?   ?      ?   -
    ? ? ? ?    ? ? | .
     ?   ?      ?   -
    ? ? ? ?    ? ? . |
     ?   ?      ?   -
    15 30
     ?   ?      ?   ?
    ? ? ? ?    ? ? ? |
     ?   ?      ?   ?
    ? ? ? ?    ? ? ? ?
     ?   ?      ?   ?
    0
    
    Expected output
    23:40
    0:05
    ambiguous, 13 possibilities
    
  2. Example 2

    Input
    1
     .   -      -   .
    . | . |    | | | ?
     ?   ?      .   ?
    ? ? ? .    | ? ? ?
     ?   ?      ?   ?
    0
    
    Expected output
    12:04
    
  3. Example 3

    Input
    2
     .   -      -   -
    . | . |    | | | ?
     ?   ?      .   ?
    ? ? ? .    | ? ? ?
     ?   ?      ?   ?
    0 4
     ?   ?      .   -
    ? ? ? ?    ? | . |
     ?   ?      ?   -
    ? ? ? ?    ? ? ? |
     ?   ?      ?   -
    0
    
    Expected output
    12:09
    12:13
    
  4. Example 4

    Input
    1
     .   -     -   -
    . . . |    | . . |
     .   .     -   -
    . . . |    . | . |
     .   .     -   -
    0
    
    Expected output
    7:53
    
  5. Example 5

    Input
    1
     -   -     -   -
    . | . |    | . | |
     -   -     -   -
    | . . |    . | . |
     -   -     -   -
    0
    
    Expected output
    23:59