Coded Communication

Interview

Time limit1sMemory limit128 MB

Summary
Given n binary strings of length b and a received string r, find the minimum Hamming distance from r to any of the n strings.
Level

Easy3 of 10

Topics
String, Brute force, Implementation, Bit manipulation
Solved
No attempts yet

Problem

When you send data over a distance — even a short one — the bits you transmit are sometimes flipped by accident. Such errors can be serious; imagine, for instance, misreading a critical instruction. To guard against them, most long-range and wireless communication uses error-correcting codes.

The simplest example that captures the idea is this: to send a single bit, '0' or '1', you can replace it with '000' or '111', respectively. Then, even if at most one bit is flipped in transit, the receiver can still recover whether the original bit was '0' or '1'. Codes based on plain duplication are not very efficient, and designing codes that add as few extra bits as possible while tolerating as many bit flips as possible is an active area of research.

Here you solve a much easier problem. Given a code someone has already designed and a received codeword, determine how many bits must have been flipped during transmission. More precisely, you are given 1≤n≤10001 \le n \le 1000 candidate strings mim_i of zeros and ones — the valid messages — each exactly bb bits long (1≤b≤1001 \le b \le 100). You are also given the received message rr, another bit string of bb bits. Find the minimum number ff of bits of rr you would need to flip in order to obtain some mim_i.

Input

The first line contains the number KK of data sets. The KK data sets follow, each of the form below.

The first line of a data set contains the two integers nn and bb. This is followed by nn lines, each describing one valid codeword as a string of bb zeros and ones. After these nn lines comes one more line containing the received string rr, again a string of bb zeros and ones.

Output

For each data set, print Data Set x: on a line by itself, where xx is its number (starting from 1). On the next line, print the minimum distance ff between the received string and any valid codeword. Separate consecutive data sets with a single blank line.

Examples4

  1. Example 1

    Input
    2
    3 3
    000
    111
    110
    010
    4 2
    00
    01
    10
    11
    00
    
    Expected output
    Data Set 1:
    1
    
    Data Set 2:
    0
    
  2. Example 2

    Input
    1
    1 1
    0
    1
    
    Expected output
    Data Set 1:
    1
    
  3. Example 3

    Input
    1
    3 5
    11111
    00000
    10101
    10101
    
    Expected output
    Data Set 1:
    0
    
  4. Example 4

    Input
    1
    2 4
    0000
    1111
    1010
    
    Expected output
    Data Set 1:
    2