Road Series
Time limit1sMemory limit128 MB
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 , , , and , but not (the two digits are separated by a dash) nor (separated by a space). They may also use the single digits , , , , , , , and the three-digit number . 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 the last complete number: the largest value such that every number from to has already been found. (Initially the last complete number is .) They also allow themselves to remember numbers they have already seen that are not too far beyond : specifically, they can remember any seen number in the window up to , where is a fixed window size. A number that is greater than at the moment it is seen is not remembered.
For example, suppose and the last complete number is , so numbers up to can be remembered. On the sign Show time at 8:25, no one under 21 admitted, they can use the but not the (it exceeds ). If the next sign is The FleaBag Hotel, phone 555-2520, its makes complete and then (already remembered) complete, so the last complete number becomes ; now the window reaches , and because also appears on that same sign, it can be used as well.
Input
The first line contains an integer , the number of test cases. Each test case begins with a line containing two positive integers and , where () is the number of signs and () is the window size. The next 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 .
Output
For each test case, output one line in the form Case i: n h, where is the test case number (starting from ), is the last complete number obtainable from that test case's signs, and is the highest number still remembered within the window (equal to when nothing beyond is remembered).