Road Series

Time limit1sMemory limit128 MB

Summary
Given signs of text processed in order, track how far the consecutive count from 1 reaches while remembering seen numbers only within a sliding window.
Level

Medium6 of 10

Topics
Simulation, Hash map, String, Implementation
Solved
No attempts yet

Problem

Don and Jan spend a lot of time together on the road, and to pass the time they play a game called Road Series. The goal is to find the number 1 on some sign, then 2, then 3, and so on. Multi-digit numbers must appear with their digits directly next to each other on a sign, and a single sign may supply several numbers.

For example, from a sign showing 678-43 15, they may use 6767, 7878, 4343, and 1515, but not 8484 (the two digits are separated by a dash) nor 3131 (separated by a space). They may also use the single digits 66, 77, 88, 44, 33, 11, 55, and the three-digit number 678678. In general, a number is usable from a sign exactly when its digits appear as a contiguous block within the sign's text.

Requiring the numbers to be found strictly in order made the game very slow, so they relaxed it. Call nn the last complete number: the largest value such that every number from 11 to nn has already been found. (Initially the last complete number is 00.) They also allow themselves to remember numbers they have already seen that are not too far beyond nn: specifically, they can remember any seen number in the window up to n+wn + w, where ww is a fixed window size. A number that is greater than n+wn + w at the moment it is seen is not remembered.

For example, suppose w=4w = 4 and the last complete number is 1919, so numbers up to 2323 can be remembered. On the sign Show time at 8:25, no one under 21 admitted, they can use the 2121 but not the 2525 (it exceeds 2323). If the next sign is The FleaBag Hotel, phone 555-2520, its 2020 makes 2020 complete and then 2121 (already remembered) complete, so the last complete number becomes 2121; now the window reaches 2525, and because 2525 also appears on that same sign, it can be used as well.

Input

The first line contains an integer mm, the number of test cases. Each test case begins with a line containing two positive integers kk and ww, where kk (k≤1000k \le 1000) is the number of signs and ww (w≤100w \le 100) is the window size. The next kk lines each contain the text of one sign. A sign's text may contain any combination of alphanumeric characters, punctuation, and spaces, and has length at most 10001000.

Output

For each test case, output one line in the form Case i: n h, where ii is the test case number (starting from 11), nn is the last complete number obtainable from that test case's signs, and hh is the highest number still remembered within the window (equal to nn when nothing beyond nn is remembered).

Examples1

  1. Example 1

    Input
    4
    2 10
    13
    2
    2 10
    2
    13
    1 8
    Tomorrow, from 12 to 2, 4-4 basketball tournament! $3 entry fee.
    1 8
    Tomorrow, from 11 to 7, 4-4 basketball tournament! $3 entry fee.
    
    Expected output
    Case 1: 3 3
    Case 2: 3 13
    Case 3: 4 12
    Case 4: 1 7