This page is still under construction.

Parts of this page are still being built. What you see may change.

Ancient Scrolls

Time limit8sMemory limit256 MB

Summary
Recover the lexicographically smallest string within Hamming distance d of three given strings, or print -1 when no such string exists.
Level

Hard8 of 10

Topics
Greedy, String, Combinatorics
Solved
No attempts yet

Problem

You bought three ancient scrolls from a magician. Each scroll carries one long string, and the three strings have the same length. The magician said the strings are copies of the key string that opens a dungeon holding a hidden treasure. People copied them by hand many times, so the strings contain errors and only the length is right.

Recover the original string from the three copies. Use these two assumptions.

  • One copy contains at most dd errors. That is, the Hamming distance between the original string and each copy is at most dd.
  • If several strings satisfy the condition, the original string is the lexicographically smallest one among them.

The Hamming distance between two strings of the same length is the number of positions where the characters differ. The original string also consists of upper and lower case English letters, and strings compare by ASCII order, so every upper case letter comes before every lower case letter.

Input

The input holds several datasets. Each dataset has this format:

l d
str1
str2
str3

The first line holds two integers ll (1≤l≤1000001 \le l \le 100000) and dd (0≤d≤50000 \le d \le 5000). ll is the length of the three strings and dd is the largest acceptable Hamming distance. The next three lines hold the three strings, each of length ll. The strings consist of upper and lower case English letters only.

The input ends with a line holding two zeros. Do not process that line.

Output

For each dataset, print the lexicographically smallest string that satisfies the condition on its own line. If no such string exists, print -1.

Examples2

  1. Example 1

    Input
    3 1
    ACM
    IBM
    ICM
    5 2
    iwzwz
    iziwi
    zwizi
    1 0
    A
    B
    C
    10 5
    jLRNlNyGWx
    yyLnlyyGDA
    yLRnvyyGDA
    0 0
    
    Expected output
    ICM
    iwiwi
    -1
    AARAlNyGDA
    
  2. Example 2

    Input
    5 2
    abcde
    abcde
    abcde
    5 0
    abcde
    abcde
    abcde
    0 0
    
    Expected output
    AAcde
    abcde