Melody

Time limit1sMemory limit128 MB

Summary
Given N notes with S-digit codes and a target tune of length L, choose a sequence of notes with adjacent Hamming distance at most G that minimizes total mismatch with the written tune, then output the smallest such sequence lexicographically.
Level

Medium6 of 10

Topics
Dynamic programming, String, Graph
Solved
No attempts yet

Problem

Linas plays a peculiar wind instrument. It has SS holes, and Linas can play NN different notes (numbered from 11 to NN). Each note is produced by covering all holes in one specific way, described by a sequence of SS digits: the jj-th digit tells how the jj-th hole must be covered, using one of 1010 coverings labelled 00 to 99. If the holes are covered in a way that matches no note, the instrument makes an unpleasant noise, so Linas always covers the holes for some valid note.

Linas wants to play a tune: a sequence of LL notes. However, he is not perfect. He can move from one note to the next only if the second note differs from the first in at most GG holes (that is, their digit sequences differ in at most GG positions). Because of this he sometimes has to play a note different from the one written in the tune. Every position where the note he plays differs from the written note is called a mistake.

Given the written tune, choose which notes Linas should actually play so that the number of mistakes is as small as possible, while every two consecutive played notes differ in at most GG holes.

Input

The first line contains three integers NN, SS, and GG (1≤N≤1001 \le N \le 100, 0≤G<S≤1000 \le G < S \le 100): the number of notes, the number of holes, and how many holes Linas may change between consecutive notes.

Each of the next NN lines contains one note as SS digits with no spaces; the jj-th digit is the covering of the jj-th hole for that note (each digit is between 00 and 99). No two notes are identical.

The next line contains one integer LL (1≤L≤1051 \le L \le 10^5): the length of the tune.

The last line contains LL integers between 11 and NN, separated by single spaces: the notes of the written tune, in order.

Output

Print two lines.

The first line contains one non-negative integer: the minimum possible number of mistakes.

The second line contains LL integers separated by single spaces: the notes Linas should actually play. They must form a valid tune (every two consecutive notes differ in at most GG holes) that attains this minimum number of mistakes. If several such tunes exist, print the lexicographically smallest one, comparing the tunes as their sequences of note numbers.

Examples6

  1. Example 1

    Input
    5 4 2
    1111
    2101
    2000
    0100
    0000
    7
    1 5 4 5 3 2 1
    
    Expected output
    1
    1 2 4 5 3 2 1
    
  2. Example 2

    Input
    1 3 0
    012
    4
    1 1 1 1
    
    Expected output
    0
    1 1 1 1
    
  3. Example 3

    Input
    3 2 1
    00
    11
    01
    1
    2
    
    Expected output
    0
    2
    
  4. Example 4

    Input
    3 2 0
    00
    11
    22
    5
    1 2 2 3 3
    
    Expected output
    3
    2 2 2 2 2
    
  5. Example 5

    Input
    4 3 1
    000
    001
    011
    111
    6
    1 2 3 4 3 2
    
    Expected output
    0
    1 2 3 4 3 2
    
  6. Example 6

    Input
    4 2 1
    00
    01
    10
    11
    5
    1 4 1 4 1
    
    Expected output
    2
    1 1 1 1 1