#P7254. [BalticOI 2012] 旋律 (Day2)

[BalticOI 2012] 旋律 (Day2)

Description

There is a little-known musical instrument. It has SS holes, and by covering these holes in ten different ways (0(0 to 9)9), it can produce NN notes. You are not allowed to use any hole-covering pattern other than those for these NN notes.

Now you want to play a piece of music, but because this instrument requires that between any two consecutive notes, the hole-covering patterns differ in at most GG holes, you may have to play some notes incorrectly. You want to minimize the number of notes you play incorrectly.

Input Format

The first line contains three integers N,S,G (0G<S100)N, S, G\ (0 \le G < S \le 100), as described above.

The next NN lines each contain a string of length SS (digits only), representing the hole-covering pattern when playing that note.

The next line contains an integer LL, the number of notes in the piece.

The last line contains LL integers, representing the indices of the notes in the piece.

Output Format

The first line contains one integer, the minimum possible number of notes that you play incorrectly.

The next line contains LL integers, representing any one valid construction of your playing plan.

5 4 2
1111
2101
2000
0100
0000
7
1 5 4 5 3 2 1
1
1 2 4 5 3 2 1

Hint

Sample Explanation

Note 11 and note 55 have different playing patterns on all four holes, so you cannot play note 55 right after playing note 11.

Constraints

  • For 40%40\% of the testdata, L100L \leq 100.
  • For 65%65\% of the testdata, L5000L \leq 5000.
  • For 100%100\% of the testdata, 1N1001 \leq N \leq 100, 1L1051 \le L \le 10 ^ 5.

Notes

Translated from BalticOI 2012 Day2 T2. Melody.

Translated by ChatGPT 5