A freight dispatcher receives n manifest lines from a scanner. Every line in this batch uses the exact same single delimiter character to separate item codes; that delimiter is given to you alongside the lines. To parse one line, split it wherever the delimiter character occurs; a token is a maximal run of characters between two delimiter occurrences (or between a line boundary and a delimiter occurrence). Any empty token -- produced by two consecutive delimiters, or by a delimiter sitting at the very start or end of a line -- is discarded and never appears in the output. Process the lines in the order given, and within each line list its surviving tokens left to right; output every surviving token across the whole batch, in that combined order.
Line 1: two space-separated values -- the integer n (number of manifest lines) and a single delimiter character sep. The next n lines: each manifest line, made up only of lowercase English letters and the character sep, with length between 1 and 20 inclusive.
Line 1: a single integer k, the total number of surviving tokens. Line 2: the k tokens, in order, separated by single spaces (print an empty line if k = 0).
Example 1
Input
3 . one.two.three four five..six
Expected
6 one two three four five six
Explanation
The delimiter is '.'. Line 1 "one.two.three" splits into three tokens: one, two, three. Line 2 "four" has no delimiter, so it is a single token: four. Line 3 "five..six" has two consecutive dots, which produces an empty token between them that gets discarded, leaving five and six. Concatenating in line order gives 6 tokens total: one two three four five six.
Example 2
Input
4 # # ## a##b c
Expected
3 a b c
Explanation
The delimiter is '#'. Line 1 "#" splits into two empty tokens (before and after the single delimiter), both discarded. Line 2 "##" splits into three empty tokens, all discarded. Line 3 "a##b" splits into a, an empty token (discarded), and b. Line 4 "c" has no delimiter, so it is the single token c. The surviving tokens in order are a, b, c -- 3 tokens total.
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 →