A monitoring station recorded one long transmission as a single lowercase string. Engineers keep a fixed list of known codewords they are watching for. Whenever a contiguous block of the transmission exactly equals one of the codewords, that block counts as a detected span, identified by its inclusive 0-indexed start and end position within the transmission. The same codeword can be detected many times at different positions, spans from different codewords may overlap or nest inside one another, and the codeword list may itself contain repeats — but each distinct (start, end) span must be reported only once even if several codewords (or duplicate entries) all match that exact span. Report every detected span, sorted first by ascending start position and, among spans sharing a start, by ascending end position.
Line 1: the transmission string text (1 <= length <= 200), lowercase English letters only.
Line 2: an integer n (1 <= n <= 50), the number of codewords.
Next n lines: one codeword each, lowercase English letters only, each of length between 1 and 50.
First line: an integer k, the number of distinct detected spans.
Next k lines: two space-separated integers start end for each span, in the required sorted order. If no span is detected, print 0 and nothing else.
Example 1
Input
themissionlogbegins 4 mission log begins miss
Expected
4 3 6 3 9 10 12 13 18
Explanation
The transmission is "themissionlogbegins". "miss" matches positions 3-6, "mission" matches positions 3-9 (overlapping with "miss" since both start where the word 'mission' begins), "log" matches positions 10-12, and "begins" matches positions 13-18. Sorted by start then end gives spans (3,6), (3,9), (10,12), (13,18).
Example 2
Input
aaa 3 a aa aaa
Expected
6 0 0 0 1 0 2 1 1 1 2 2 2
Explanation
The transmission is "aaa" (positions 0,1,2). Codeword "a" matches each single position: (0,0),(1,1),(2,2). Codeword "aa" matches the two overlapping pairs: (0,1),(1,2). Codeword "aaa" matches the whole string: (0,2). Sorted by start then end: (0,0),(0,1),(0,2),(1,1),(1,2),(2,2).
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 →