A warehouse conveyor carries a fixed sequence of totes down the line. Each tote holds zero or more item codes, always scanned in the fixed order they were packed. Build a scanning head that walks across every tote from first to last, treating all of their item codes as one continuous stream (empty totes contribute nothing and are skipped entirely). The scanning head must answer two kinds of queries: whether the stream has another item remaining, and -- advancing the head one position -- what the next item code in the stream is.
m -- the number of totes.m lines each describe one tote: an integer k_i (the number of item codes it holds) followed by k_i space-separated integers (its item codes, in packed order). If k_i is 0, no integers follow on that line.q -- the number of queries.q lines each contain one query, either HASNEXT or NEXT.It is guaranteed that a NEXT query is never issued when the stream has no items remaining.
For every query, in order, print one line:
HASNEXT, print true or false.NEXT, print the integer item code that the scan advances to and returns.Example 1
Input
3 2 1 2 0 1 3 5 HASNEXT NEXT NEXT HASNEXT NEXT
Expected
true 1 2 true 3
Explanation
The totes hold [1,2], [] (empty), [3]. HASNEXT: the stream still has items, so true. NEXT: returns 1 (first item of tote 1). NEXT: returns 2 (second item of tote 1; tote 1 is now exhausted). HASNEXT: the head must skip over the empty second tote to find tote 3's item, so it reports true. NEXT: returns 3 (the only item of tote 3). Output: true, 1, 2, true, 3.
Example 2
Input
2 0 0 1 HASNEXT
Expected
false
Explanation
Both totes are empty, so the flattened stream has no items at all. HASNEXT -> false.
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 →