Moo Sick
InterviewTime limit1sMemory limit128 MB
Find every window of C consecutive notes whose sorted, min-subtracted shape matches the given chord's shape.
- Level
Medium4 of 10
- Topics
- Array, Sorting, Sliding window, Implementation
- Solved
- No attempts yet
Problem
Cows love almost every kind of music. Almost -- the great cow composer Wolfgang Amadeus Moozart once discovered that one particular chord tends to make cows ill. That chord, the ruminant seventh chord, is therefore avoided in cow compositions.
Farmer John, unaware of this, plays his favorite song over the barn loudspeakers. Your task is to find every ruminant seventh chord in the song so we can estimate how sick it will make the cows.
The song is a sequence of notes (), each an integer in . A ruminant seventh chord is defined by a set of distinct notes (), also integers in . The chord is invariant under transposition (adding the same amount to every note) and reordering. For example, if 4 6 7 is a ruminant seventh chord, then 3 5 6 (transposed by ), 6 8 9 (transposed by ), 6 4 7 (reordered), and 5 3 6 (transposed and reordered) are all ruminant seventh chords too.
An occurrence of the chord in the song is any block of consecutive notes that, after some transposition and reordering, equals the chord. Such an occurrence is uniquely identified by its starting position. Report the starting positions of every ruminant seventh chord occurrence in the song.
Input
- Line 1: the integer .
- Lines 2 to : the notes of the song, one per line.
- Line : the integer .
- Lines to : the notes of one example ruminant seventh chord, one per line. Every transposition and/or reordering of these notes is also a ruminant seventh chord.
Output
- Line 1: the count of ruminant seventh chord occurrences in the song. Note that different occurrences may overlap.
- Lines 2 to : the starting index of each occurrence (index is the first note, index the last), listed in increasing order.
Hint
To test a block of consecutive notes, sort it and subtract the smallest note from every note; this yields a "shape" that is invariant to transposition and reordering. Compute the chord's shape the same way. The block is a ruminant seventh chord exactly when its shape equals the chord's shape. Because occurrences are counted at every starting position, two of them may overlap.