Baltic Olympiad in Informatics: 2012 Day 2, Problem 2
Linas likes to play some musical instrument, and nobody knows what it is called. The instrument has holes and Linas is able to play different notes (numbered from to ) by covering each hole in one of different ways (numbered from to ). Every note can be played by covering all holes in exactly one way, described by a sequence of digits corresponding to coverings of respective holes. If the holes are covered incorrectly (i.e., not corresponding to any note), the instrument starts to produce very unpleasant sounds, so Linas plays a wrong note rather than covers holes incorrectly.
Linas plays in a band where he has to play complicated tunes very quickly. Linas has written a tune (i.e., a sequence of numbers, corresponding to notes) and he wants to play it together with the band. Unfortunately, Linas doesn't play perfectly. He can only play two successive notes if by playing the second he has to cover no more than holes differently than when playing the first one. Hence he decided to sometimes play a wrong note in the tune. Each incorrect note Linas plays is called mistake.
For a given tune find a modified tune that Linas can play making the least possible number of mistakes.
Constraints
In test cases worth of points, .
In test cases worth of points, .
Input Specification
First line of input contains three space-separated integers: the number of possible notes , number of holes , and Linas' finger speed .
Next lines contain the list of possible notes. There are digits without spaces on each of them. The digit in the line corresponds to covering of the hole required to play the note (the hole can be covered in various ways, labelled by digits from to ). No two notes are played in the same way.
The line contains the length of the tune .
The last line contains the tune: space-separated integers, corresponding to the notes played successively in the tune.
Output Specification
The first line of output must contain one non-negative integer – the minimum number of mistakes.
The second line must contain a valid tune which obtains the minimum number of mistakes: space-separated integers, corresponding to the notes that Linas should play. If there are multiple such tunes, output any of them.
Sample Input
5 4 2
1111
2101
2000
0100
0000
7
1 5 4 5 3 2 1
Sample Output
1
1 2 4 5 3 2 1
Sample Explanation
Linas can't play note directly after note .
Comments