Under construction

Still being built. Things may change

Improvisation

Time limit1sMemory limit128 MB

Summary
Given a chord progression and a melody with pauses, remap each played note to the nearest note in the current chord's scale, advancing the chord every fourth position.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array, String
Solved
No attempts yet

Problem

Chris always dreamed of playing jazz. He has grown up but still cannot play a single instrument, so instead he wants to build a new kind of improvisation engine. His program, Improvisation, takes an improvisation that fits one chord progression and rewrites it so that it fits a different chord progression. Your task is to implement that algorithm.

A few definitions are needed first.

Notes. There are 12 notes, arranged in a fixed classical order:

C, C#(D♭), D, D#(E♭), E, F, F#(G♭), G, G#(A♭), A, A#(B♭), B

The order is cyclic: B is followed by C again. Each of the five notes written with a sharp (#) can also be written with a flat (♭).

Pause. A pause is written as _.

Chords. Every chord has a mode and a prime. The prime is a single note. There are six modes: maj7, min7, dim, 7, 7♭9, and 7#9. Each mode defines a scale: a set S⊆{0,1,…,11}S \subseteq \{0, 1, \dots, 11\} of note offsets relative to the prime in the classical order. For example, if the prime is D and S={0,2,4}S = \{0, 2, 4\}, then the fitting notes are {D,E,F#}\{D, E, F\#\}. The scales are:

modescale
maj70, 2, 4, 7, 9, 11
min70, 2, 3, 5, 7, 9, 10
dim0, 2, 3, 5, 6, 8, 9, 11
70, 2, 4, 7, 9, 10
7♭90, 1, 3, 4, 6, 7, 9, 10
7#90, 1, 3, 4, 6, 8, 10

A chord is written as its prime and mode joined into one word (for example, Cmaj7). The fitting notes of Cmaj7 are {C,D,E,G,A,B}\{C, D, E, G, A, B\}.

Improvisation. An improvisation is a finite sequence of notes and pauses.

Nearest fitting note. For a note xx and a chord AA, the nearest note that fits is the first element of the sequence

x, next(x), prev(x), next(next(x)), prev(prev(x)), …x,\ \text{next}(x),\ \text{prev}(x),\ \text{next}(\text{next}(x)),\ \text{prev}(\text{prev}(x)),\ \dots

that fits AA, where next(x)\text{next}(x) is the note right after xx in the classical order and prev(x)\text{prev}(x) is the note right before it.

The algorithm takes a chord progression PP of length mm and an improvisation II of length nn, and produces a new improvisation JJ:

 1  J := sequence of length n containing only pauses
 2  p := 1
 3  for i := 1 to n do begin
 4      if I[i] <> pause then begin
 5          J[i] := nearest note to I[i] which fits to P[p]
 6          if i mod 4 = 0 then begin
 7              p := p + 1
 8              if p > m then p := 1
 9          end
10      end
11  end

Read a chord progression and an improvisation, run the algorithm, and print the resulting improvisation JJ.

Input

The first line contains one integer mm (1≤m≤1000001 \le m \le 100000). The second line contains mm chords separated by single spaces. The third line contains one integer nn (1≤n≤1000001 \le n \le 100000). The fourth line contains nn notes and/or pauses separated by single spaces, forming the improvisation.

In the input a sharp is written as # and a flat as the lowercase letter b: for example G♭ is written Gb, and the modes 7♭9 and 7#9 are written 7b9 and 7#9. A pause is _.

Output

Print one line with nn notes and/or pauses separated by single spaces: the resulting improvisation JJ.

When a note has two spellings (a sharp note), print it using the spelling implied by the chord P[p]P[p] that produced it on line 5 of the algorithm. If that chord's prime is written with a flat, use the flat spelling; otherwise use the sharp spelling. As in the input, a sharp is # and a flat is b.

Examples2

  1. Example 1

    Input
    2
    Gb7 Bb7
    12
    C C# D D# E F _ F# G _ G# A
    
    Expected output
    Db Db Eb Eb F F _ G Ab _ Ab Bb
    
  2. Example 2

    Input
    1
    Db7
    4
    C F Gb A
    
    Expected output
    Db F F Bb