Problem:
Problem Statement
You bought 3 ancient scrolls from a magician. These scrolls have a long string, and the lengths of the strings are the same. He said that these scrolls are copies of the key string to enter a dungeon with a secret treasure.
However, he also said, they were copied so many times by hand, so the string will contain some errors, though the length seems correct.
Your job is to recover the original string from these strings. When finding the original string, you decided to use the following assumption.
The copied string will contain at most
d
errors. In other words, the Hamming distance of the original string and the copied string is at most
d
.
If there exist many candidates, the lexicographically minimum string is the original string.
Can you find the orignal string?
Input
The input contains a series of datasets.
Each dataset has the following format:
l
d
str_1
str_2
str_3
The first line contains two integers
l
(
1 \leq l \leq 100,000
) and
d
(
0 \leq d \leq 5,000
.)
l
describes the length of 3 given strings and
d
describes acceptable maximal Hamming distance.
The following 3 lines have given strings, whose lengths are
l
.
These 3 strings consist of only lower and upper case alphabets.
The input ends with a line containing two zeros, which should not be processed.
Output
Print the lexicographically minimum satisfying the condition in a line.
If there do not exist such strings, print
-1
.
Sample Input
3 1
ACM
IBM
ICM
5 2
iwzwz
iziwi
zwizi
1 0
A
B
C
10 5
jLRNlNyGWx
yyLnlyyGDA
yLRnvyyGDA
0 0
Output for the Sample Input
ICM
iwiwi
-1
AARAlNyGDA

Submitted solutions:

code_snippet: #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=100010;
char A[maxn],B[maxn],C[maxn],ans[maxn],temp[4];
int t[maxn],can[3],n,d,type[5];
bool check()
{
    int now[3],tt[5],x;
    for(int i=0;i<3;i++) now[i]=can[i];
    for(int i=0;i<5;i++) tt[i]=type[i];

    x=min(now[2],tt[1]);
    now[2]-=x;tt[1]-=x;
    now[1]-=tt[1],now[0]-=tt[1];
    if(now[0]<0||now[1]<0) return false;

    x=min(now[1],tt[2]);
    now[1]-=x;tt[2]-=x;
    now[0]-=tt[2];now[2]-=tt[2];
    if(now[0]<0||now[2]<0) return false;

    x=min(now[0],tt[3]);
    now[0]-=x;tt[3]-=x;
    now[1]-=tt[3];now[2]-=tt[3];
    if(now[1]<0||now[2]<0) return false;

    sort(now,now+3);
    x=min(now[0]+now[1],(now[0]+now[1]+now[2])/2);
    if(x>=tt[4]) return true;
    else return false;
}
int main()
{
    //freopen("data.in","r",stdin);
    while(scanf("%d%d",&n,&d)&&(n||d))
    {
        scanf("%s%s%s",A,B,C);
        memset(type,0,sizeof(type));
        for(int i=0;i<n;i++)
        {
            if(A[i]==B[i]&&B[i]==C[i]) t[i]=0;
            else if(A[i]==B[i]) t[i]=1;
            else if(A[i]==C[i]) t[i]=2;
            else if(B[i]==C[i]) t[i]=3;
            else t[i]=4;
            type[t[i]]++;
        }
        for(int i=0;i<3;i++) can[i]=d;
        if(!check())
            printf("-1\n");
        else
        {
            for(int i=0;i<n;i++)
            {
                type[t[i]]--;
                temp[0]='A';temp[1]=A[i];temp[2]=B[i];temp[3]=C[i];
                sort(temp,temp+4);
                for(int j=0;j<4;j++)
                {
                    ans[i]=temp[j];
                    if(ans[i]!=A[i])can[0]--;
                    if(ans[i]!=B[i])can[1]--;
                    if(ans[i]!=C[i])can[2]--;
                    if(check()) break;
                    if(ans[i]!=A[i])can[0]++;
                    if(ans[i]!=B[i])can[1]++;
                    if(ans[i]!=C[i])can[2]++;
                }
            }
            ans[n]=0;
            printf("%s\n",ans);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3964
final_rank: 9

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

bool satisfiable(int a, vector<int> b, int c, vector<int> d) {
    for(int i : rep(3)) if(not(0 <= d[i])) return false;
    for(int i : rep(3)) {
        if(not(b[i] <= d[i])) {
            for(int j : rep(3)) if(j != i) {
                d[j] -= (b[i] - d[i]);
            }
            b[i] = d[i];
        }
    }
    
    int sum = 0;
    for(int i : rep(3)) sum += max(a - (d[i] - b[i]), 0);
    return sum <= a;
}

string solve(int n, int d) {
    vector<string> s = in(3);

    int a = 0; vector<int> b(3, 0); int c = 0;
    auto push = [&](char c0, char c1, char c2, int x = +1) {
        if(c0 == c1 and c1 == c2) c += x;
        else if(c1 == c2) b[0] += x;
        else if(c0 == c2) b[1] += x;
        else if(c0 == c1) b[2] += x;
        else a += x;
    };
    for(int i : rep(n)) push(s[0][i], s[1][i], s[2][i]);
    vector<int> dnow(3, d);

    if(!satisfiable(a, b, c, dnow)) return "-1";

    string ans(n, '?');
    for(int i : rep(n)) {
        string can = ""s + 'A' + s[0][i] + s[1][i] + s[2][i];
        unique(can);
        for(char c : can) {
            push(s[0][i], s[1][i], s[2][i], -1);
            if(s[0][i] != c) dnow[0]--;
            if(s[1][i] != c) dnow[1]--;
            if(s[2][i] != c) dnow[2]--;

            if(satisfiable(a, b, c, dnow)) { ans[i] = c; break; }

            push(s[0][i], s[1][i], s[2][i], +1);
            if(s[0][i] != c) dnow[0]++;
            if(s[1][i] != c) dnow[1]++;
            if(s[2][i] != c) dnow[2]++;
        }
    }
    
    return ans;
}

int main() {
    while(true) {
        int l = in(), d = in();
        if(make_pair(l, d) == make_pair(0, 0)) return 0;
        print(solve(l, d));
    }
}
accuracy: 1
time_ms: 70
memory_kb: 3716
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N,D;cin>>N>>D;
        if(N==0) break;
        vector<string> S(3);
        for(int i=0;i<3;i++) cin>>S[i];
        
        auto type=[&](char a,char b,char c){
            if(a==b&&b==c) return 4;
            if(b==c) return 0;
            if(a==c) return 1;
            if(a==b) return 2;
            return 3;
        };
        
        auto f=[&](int j){
            return type(S[0][j],S[1][j],S[2][j]);
        };
        
        vector<int> cn(5);
        
        for(int j=0;j<N;j++) cn[f(j)]++;
        
        vector<int> allow(3,D);
        
        auto check=[&](){
            int X=allow[0];
            if(X<0) return false;
            int Y=cn[0]+cn[1]+cn[3]-allow[1];
            int Z=cn[0]+cn[2]+cn[3]-allow[2];
            if(Y<=0&&Z<=0) return true;
            
            int s=min(X,cn[0]);
            X-=s;Y-=s;Z-=s;
            if(Y<=0&&Z<=0) return true;
            
            if(Y<=0){
                s=min(X,cn[3]);
                X-=s;Z-=s;
                if(Z<=0) return true;
                s=min({X,abs(Y),cn[2]});
                Z-=s;
                if(Z<=0) return true;
                else return false;
            }else if(Z<=0){
                s=min(X,cn[3]);
                X-=s;Y-=s;
                if(Y<=0) return true;
                s=min({X,abs(Z),cn[1]});
                Y-=s;
                if(Y<=0) return true;
                else return false;
            }else{
                s=min(X,cn[3]);
                if(Y+Z>s) return false;
                else return true;
            }
        };
        
        if(!check()){
            cout<<-1<<"\n";
            continue;
        }
        
        string ans;
        
        for(int j=0;j<N;j++){
            int x=f(j);
            string use="A";
            for(int i=0;i<3;i++) use+=S[i][j];
            sort(all(use));
            use.erase(unique(all(use)),use.end());
            
            cn[x]--;
            
            for(char c:use){
                for(int i=0;i<3;i++){
                    if(c!=S[i][j]) allow[i]--;
                }
                if(check()){
                    ans+=c;
                    break;
                }
                for(int i=0;i<3;i++){
                    if(c!=S[i][j]) allow[i]++;
                }
            }
        }
        
        cout<<ans<<"\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3660
final_rank: 5

code_snippet: #line 1 "a.cpp"
// cSpell:disable
/*	author:  Kite_kuma
	created: 2022.11.22 06:01:20 */

#line 2 "SPJ-Library/template/kuma.hpp"

#line 2 "SPJ-Library/template/basic_func.hpp"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool flag = true) { std::cout << (flag ? "Yes" : "No") << '\n'; }
void YES(bool flag = true) { std::cout << (flag ? "YES" : "NO") << '\n'; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(const T &x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(const Head &H, const Tail &... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T value) {
	for(auto &a : v) a += value;
	return;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

// ceil(a / b);
template <class T>
T dup(T a, T b) {
	assert(b);
	if(b < 0) {
		a *= -1;
		b *= -1;
	}
	return least_upper_multiple(a, b) / b;
}

long long pow_ll(long long a, long long n) {
	assert(n >= 0LL);
	if(n == 0) return 1LL;
	if(a == 0) return 0LL;
	if(a == 1) return 1LL;
	if(a == -1) return (n & 1LL) ? -1LL : 1LL;
	long long res = 1;
	while(n > 1LL) {
		if(n & 1LL) res *= a;
		a *= a;
		n >>= 1;
	}
	return res * a;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, const long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(const std::vector<T> &a) {
	std::vector<T> vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(const auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}
#line 1 "SPJ-Library/template/header.hpp"
#include <bits/stdc++.h>
#line 2 "SPJ-Library/template/io.hpp"

#line 4 "SPJ-Library/template/io.hpp"

#line 8 "SPJ-Library/template/debug.hpp"

#line 3 "SPJ-Library/template/constants.hpp"

constexpr int inf = 1000'000'000;
constexpr long long INF = 1'000'000'000'000'000'000LL;
constexpr int mod_1000000007 = 1000000007;
constexpr int mod_998244353 = 998244353;
const long double pi = acosl(-1.);
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#line 10 "SPJ-Library/template/debug.hpp"

namespace viewer {

template <typename T, typename U>
void view(const std::pair<T, U> &p);

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p);

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p);

void view(const long long &e);

void view(const int &e);

template <typename T>
void view(const T &e);

template <typename T>
void view(const std::set<T> &s);

template <typename T>
void view(const std::unordered_set<T> &s);

template <typename T>
void view(const std::multiset<T> &s);

template <typename T>
void view(const std::unordered_multiset<T> &s);

template <typename T>
void view(const std::vector<T> &v);

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v);

template <typename T>
void view(const std::vector<std::vector<T>> &vv);

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v);

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v);

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);

template <typename map_type>
void view_map_container(const map_type &m);

template <typename T, typename U>
void view(const std::map<T, U> &m);

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m);

template <typename container_type>
void view_container(const container_type &c, bool vertically = false) {
	typename container_type::const_iterator begin = c.begin();
	const typename container_type::const_iterator end = c.end();
	if(vertically) {
		std::cerr << "{\n";
		while(begin != end) {
			std::cerr << '\t';
			view(*(begin++));
			if(begin != end) std::cerr << ',';
			std::cerr << '\n';
		}
		std::cerr << '}';
		return;
	}
	std::cerr << "{ ";
	while(begin != end) {
		view(*(begin++));
		if(begin != end) std::cerr << ',';
		std::cerr << ' ';
	}
	std::cerr << '}';
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << '(';
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ')';
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ')';
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ')';
}

void view(const long long &e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int &e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T &e) {
	std::cerr << e;
}

template <typename T>
void view(const std::set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::vector<T> &v) {
	view_container(v);
}

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v) {
	view_container(v);
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	view_container(vv, true);
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	view_container(v, true);
}

template <typename map_type>
void view_map_container(const map_type &m) {
	std::cerr << "{\n";
	for(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {
		std::cerr << "\t[";
		view(it->first);
		std::cerr << "] : ";
		view(it->second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	view_map_container(m);
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	view_map_container(m);
}

}  // namespace viewer

// when compiling : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename T>
void debug_out(const T &x) {
	viewer::view(x);
}
template <typename Head, typename... Tail>
void debug_out(const Head &H, const Tail &... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "]\n";                                       \
	} while(0)
#else
#define debug(...) (void(0))
#endif
#line 2 "SPJ-Library/template/scanner.hpp"

#line 6 "SPJ-Library/template/scanner.hpp"

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#line 7 "SPJ-Library/template/io.hpp"

#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << ' ' << p.second;
	return os;
}

struct fast_io {
	fast_io() {
		std::ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		std::cout << std::fixed << std::setprecision(15);
		srand((unsigned)time(NULL));
	}
} fast_io_;
#line 2 "SPJ-Library/template/macros.hpp"

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;
#line 7 "SPJ-Library/template/kuma.hpp"

using namespace std;
#line 6 "a.cpp"

vector<int> solve(int len, int diff_max, vector<vi> scrolls) {
	debug(scrolls);
	vector<int> types(len);
	vector<int> count(5);
	rep(i, len) {
		int a = scrolls[0][i];
		int b = scrolls[1][i];
		int c = scrolls[2][i];
		auto& tp = types[i];
		if(a == c) {
			tp = (a == b) ? 4 : 1;
		} else if(a == b) {
			tp = 2;
		} else if(b == c) {
			tp = 0;
		} else {
			tp = 3;
		}
		count[tp]++;
	}
	vector<int> lifes(3, diff_max);
	auto subtracted = [&count](vector<int> vec) -> vector<int> {
		rep(i, 3) vec[i] -= count[i];
		return vec;
	};
	auto judge = [&subtracted, &count](vector<int> life, int take) -> bool {
		life = subtracted(life);
		rep(i, 3) {
			if(life[i] < 0) {
				const int flip = abs(life[i]);
				if(count[i] < flip) return false;
				rep(j, 3) { life[j] += (j == i ? flip : -flip); }
				break;
			}
		}
		int sm = accumulate(all(life), 0);
		if(sm < take * 2) return false;
		rep(i, 3) {
			if(sm - life[i] < take) return false;
			if(life[i] < 0) return false;
		}
		return true;
	};
	if(!judge(lifes, count[3])) return {};
	constexpr int chsize = 52;
	vector<int> ans(len);
	rep(i, len) {
		count[types[i]]--;
		rep(ch, chsize) {
			auto sub = lifes;
			rep(row, 3) {
				if(scrolls[row][i] != ch) {
					sub[row]--;
				}
			}
			if(judge(sub, count[3])) {
				ans[i] = ch;
				lifes = sub;
				break;
			}
		}
	}
	debug(lifes);
	return ans;
}

int main() {
	int len, d;
	while(cin >> len >> d && len + d) {
		vector<vector<int>> scrolls(3, vector<int>(len));
		string used_chars;
		rep(i, 26) { used_chars.push_back(char('A' + i)); }
		rep(i, 26) { used_chars.push_back(char('a' + i)); }
		map<char, int> rev;
		rep(i, int(used_chars.size())) rev[used_chars[i]] = i;
		foa(row, scrolls) {
			string s;
			cin >> s;
			rep(i, len) { row[i] = rev[s[i]]; }
		}
		vector<int> ans = solve(len, d, scrolls);
		if(ans.empty()) {
			cout << "-1";
		} else {
			rep(i, len) { cout << used_chars[ans[i]]; }
		}
		cout << '\n';
	}
	return 0;
}
accuracy: 1
time_ms: 940
memory_kb: 6476
final_rank: 10

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int val(vector<int> a, vector<int> b) {
	auto calc = [&](int x) {
		int x1 = min(b[2], a[1] - x);
		int x2 = min(b[1], a[2] - x);
		return x + min(x1 + x2, a[0]);
	};
	int le = 0;
	int ri = min({ a[1],a[2],b[0] });
	while (ri - le > 2) {
		int m = (le + ri) / 2;
		int c1 = calc(m);
		int c2 = calc(m + 1);
		if (c1 < c2) {
			le = m;
		}
		else {
			ri = m;
		}
	}
	int ans = 0;
	Rep1(j, le, ri) {
		chmax(ans, calc(j));
	}
	return ans;
}
//0:s[1]==s[2]
//1:s[0]==s[2]
//2:s[0]==s[1]
//3:s[0]!=s[1]!=s[2]
//4:s[0]==s[1]==s[2]
bool isok(vector<int> a, vector<int> b) {
	if (a[0] < 0 || a[1] < 0 || a[2] < 0)return false;
	int sum = b[0] + b[1] + b[2] + b[3];
	vector<int> na(3, 0);
	rep(i, 3) {
		na[i] = -(a[i] - sum);
		if (na[i] < 0)na[i] = 0;
	}
	vector<int> nb = b; nb.resize(3);
	int v = val(na, nb);
	if (na[0] + na[1] + na[2] - sum <= v)return true;
	return false;
}
int n, d;
void solve() {
	string s[3];
	rep(i, 3)cin >> s[i];
	vector<int> a(3, d);
	vector<int> b(5, 0);
	vector<int> typ(n);
	rep(i, n) {
		if (s[0][i] == s[1][i] && s[1][i] == s[2][i])typ[i] = 4;
		else if (s[1][i] == s[2][i])typ[i] = 0;
		else if (s[0][i] == s[2][i])typ[i] = 1;
		else if (s[0][i] == s[1][i])typ[i] = 2;
		else typ[i] = 3;
		b[typ[i]]++;
	}
	if (!isok(a, b)) {
		cout << -1 << "\n"; return;
	}
	string ans;
	rep(i, n) {
		b[typ[i]]--;
		bool ald = false;
		rep(j, 52) {
			char c;
			if (j < 26)c = 'A' + j;
			else c = 'a' + (j-26);
			int cnt = 0;
			rep(k, 3) {
				if (s[k][i] != c) {
					a[k]--;
					cnt++;
				}
			}
			if (cnt == 3 && ald) {
				rep(k, 3) {
					if (s[k][i] != c)a[k]++;
				}
				continue;
			}
			if (cnt == 3)ald = true;
			if (isok(a, b)) {
				ans.push_back(c);
				break;
			}
			rep(k, 3) {
				if (s[k][i] != c)a[k]++;
			}
		}
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//useexpr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> d, n) {
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 12352
final_rank: 11

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

int val(vector<int> a, vector<int> b) {
	auto calc = [&](int x) {
		int x1 = min(b[2], a[1] - x);
		int x2 = min(b[1], a[2] - x);
		return x + min(x1 + x2, a[0]);
	};
	int le = 0;
	int ri = min({ a[1],a[2],b[0] });
	while (ri - le > 2) {
		int m = (le + ri) / 2;
		int c1 = calc(m);
		int c2 = calc(m + 1);
		if (c1 < c2) {
			le = m;
		}
		else {
			ri = m;
		}
	}
	int ans = 0;
	Rep1(j, le, ri) {
		chmax(ans, calc(j));
	}
	return ans;
}
//0:s[1]==s[2]
//1:s[0]==s[2]
//2:s[0]==s[1]
//3:s[0]!=s[1]!=s[2]
//4:s[0]==s[1]==s[2]
bool isok(vector<int> a, vector<int> b) {
	if (a[0] < 0 || a[1] < 0 || a[2] < 0)return false;
	int sum = b[0] + b[1] + b[2] + b[3];
	vector<int> na(3, 0);
	rep(i, 3) {
		na[i] = -(a[i] - sum);
		if (na[i] < 0)na[i] = 0;
	}
	vector<int> nb = b; nb.resize(3);
	int v = val(na, nb);
	if (na[0] + na[1] + na[2] - sum <= v)return true;
	return false;
}
int n, d;
void solve() {
	string s[3];
	rep(i, 3)cin >> s[i];
	vector<int> a(3, d);
	vector<int> b(5, 0);
	vector<int> typ(n);
	rep(i, n) {
		if (s[0][i] == s[1][i] && s[1][i] == s[2][i])typ[i] = 4;
		else if (s[1][i] == s[2][i])typ[i] = 0;
		else if (s[0][i] == s[2][i])typ[i] = 1;
		else if (s[0][i] == s[1][i])typ[i] = 2;
		else typ[i] = 3;
		b[typ[i]]++;
	}
	if (!isok(a, b)) {
		cout << -1 << "\n"; return;
	}
	string ans;
	rep(i, n) {
		b[typ[i]]--;
		rep(j, 52) {
			char c;
			if (j < 26)c = 'A' + j;
			else c = 'a' + (j-26);
			rep(k, 3) {
				if (s[k][i] != c)a[k]--;
			}
			if (isok(a, b)) {
				ans.push_back(c);
				break;
			}
			rep(k, 3) {
				if (s[k][i] != c)a[k]++;
			}
		}
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//useexpr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> d, n) {
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 1850
memory_kb: 12380
final_rank: 13

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

bool func(int asum, int bsum, int csum, vector<int>sum) {
	if (asum > M || bsum > M || csum > M)return false;
	int aadd = sum[3];
	int badd = sum[2];
	int cadd = sum[1];
	if (asum + aadd > M) {
		int box = asum + aadd - M;
		aadd -= box;
		badd += box;
		cadd += box;
	}
	if (bsum + badd > M) {
		int box = bsum + badd - M;
		badd -= box;
		aadd += box;
		cadd += box;
	}
	if (csum + cadd > M) {
		int box = csum + cadd - M;
		cadd -= box;
		badd += box;
		aadd += box;
	}
	aadd += sum[4];
	badd += sum[4];
	cadd += sum[4];
	int amari = sum[4];
	if (asum + aadd > M) {
		int box = min(amari, asum + aadd - (int)M);
		aadd -= box;
		amari -= box;
	}
	if (bsum + badd > M) {
		int box = min(amari, bsum + badd - (int)M);
		badd -= box;
		amari -= box;
	}
	if (csum + cadd > M) {
		int box = min(amari, csum + cadd - (int)M);
		cadd -= box;
		amari -= box;
	}
	return asum + aadd <= M && csum + cadd <= M && bsum + badd <= M;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> N >> M, N) {
		string a, b, c;
		cin >> a >> b >> c;
		vector<int>num(N);
		vector<int>sum(5);
		for (int i = 0; i < N; i++) {
			if (a[i] == b[i] && a[i] != c[i])num[i] = 1;
			if (a[i] == c[i] && a[i] != b[i])num[i] = 2;
			if (b[i] == c[i] && b[i] != a[i])num[i] = 3;
			if (a[i] != b[i] && a[i] != c[i] && b[i] != c[i])num[i] = 4;
			sum[num[i]]++;
		}
		string ans = "-1";
		string ret;
		int asum = 0, bsum = 0, csum = 0;
		vector<char>check;
		for (char i = 'A'; i <= 'Z'; i++)check.push_back(i);
		for (char i = 'a'; i <= 'z'; i++)check.push_back(i);
		for (int i = 0; i < N; i++) {
			sum[num[i]]--;
			for (auto j : check) {
				asum += a[i] != j;
				bsum += b[i] != j;
				csum += c[i] != j;
			//	cout << j << " " << asum << " " << bsum << " " << csum << " " << endl;
			//	for (auto k : sum)cout << k << " ";
			//	cout << endl;
				if (func(asum, bsum, csum, sum)) {
					ret.push_back(j);
					break;
				}
				else {
					asum -= a[i] != j;
					bsum -= b[i] != j;
					csum -= c[i] != j;
				}
			}
		}
	//	cout << ret << endl;
		if (ret.size() == N)ans = ret;
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 1790
memory_kb: 3884
final_rank: 12

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

enum Type{
	ALL_SAME,
	ALL_DIFFERENT,
	SAME_01,
	SAME_02,
	SAME_12,
};


int LEN,LIMIT;
int count_table[5],rest[3],tmp[3];
char buf[3][SIZE];
Type type[SIZE];


bool isOK(){

	int plus_count = 0;
	int plus_index = -1;

	int num_0 = max(0,count_table[ALL_DIFFERENT]-(rest[0]-count_table[SAME_12]));
	int num_1 = max(0,count_table[ALL_DIFFERENT]-(rest[1]-count_table[SAME_02]));
	int num_2 = max(0,count_table[ALL_DIFFERENT]-(rest[2]-count_table[SAME_01]));

	if(num_0 > 0){
		plus_count++;
		plus_index = 0;
	}
	if(num_1 > 0){
		plus_count++;
		plus_index = 1;
	}
	if(num_2 > 0){
		plus_count++;
		plus_index = 2;
	}

	if(num_0+num_1+num_2 <= count_table[ALL_DIFFERENT])return true;
	if(plus_count >= 2)return false;

	int minus;

	switch(plus_index){
	case 0:

		minus = min(count_table[SAME_12],min(rest[1]-(count_table[ALL_DIFFERENT]+count_table[SAME_02]),
				rest[2]-(count_table[ALL_DIFFERENT]+count_table[SAME_01])));
		break;
	case 1:
		minus = min(count_table[SAME_02],min(rest[0]-(count_table[ALL_DIFFERENT]+count_table[SAME_12]),
						rest[2]-(count_table[ALL_DIFFERENT]+count_table[SAME_01])));
		break;
	case 2:
		minus = min(count_table[SAME_01],min(rest[0]-(count_table[ALL_DIFFERENT]+count_table[SAME_12]),
								rest[1]-(count_table[ALL_DIFFERENT]+count_table[SAME_02])));
		break;
	}

	return num_0+num_1+num_2-minus <= count_table[ALL_DIFFERENT];
}


void func(){

	for(int i = 0; i < 3; i++){

		scanf("%s",buf[i]);
	}

	for(int i = 0; i < 5; i++){

		count_table[i] = 0;
	}

	for(int i = 0; i < 3; i++){

		rest[i] = LIMIT;
	}

	for(int i = 0; i < LEN; i++){

		if(buf[0][i] == buf[1][i] && buf[0][i] == buf[2][i]){

			count_table[ALL_SAME] += 1;
			type[i] = ALL_SAME;

		}else if(buf[0][i] == buf[1][i]){

			count_table[SAME_01] += 1;
			type[i] = SAME_01;

		}else if(buf[0][i] == buf[2][i]){

			count_table[SAME_02] += 1;
			type[i] = SAME_02;

		}else if(buf[1][i] == buf[2][i]){

			count_table[SAME_12] += 1;
			type[i] = SAME_12;

		}else{

			count_table[ALL_DIFFERENT] += 1;
			type[i] = ALL_DIFFERENT;
		}
	}

	if(!isOK()){

		printf("-1\n");
		return;
	}


	vector<char> ANS,work;

	for(int i = 0; i < LEN; i++){

		work.clear();

		work.push_back('A');
		for(int k = 0; k < 3; k++){

			work.push_back(buf[k][i]);
		}
		sort(work.begin(),work.end());
		work.erase(unique(work.begin(),work.end()),work.end());

		count_table[type[i]] -= 1;

		for(int k = 0; k < work.size(); k++){

			for(int p = 0; p < 3; p++){

				tmp[p] = rest[p];
				if(buf[p][i] != work[k]){

					rest[p] -= 1;
				}
			}

			if(isOK()){

				ANS.push_back(work[k]);
				break;
			}

			for(int p = 0; p < 3; p++){

				rest[p] = tmp[p];
			}
		}
	}

	for(int i = 0; i < ANS.size(); i++){

		printf("%c",ANS[i]);
	}
	printf("\n");
}

int main(){

	while(true){

		scanf("%d %d",&LEN,&LIMIT);
		if(LEN == 0 && LIMIT == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3952
final_rank: 8

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
char A[110000];
char B[110000];
char C[110000];
char ret[110000];
int c[5];
int d[3];
int e[3];
int chk(){
    for(int i=0;i<3;i++)e[i]=d[i];
    int rem=c[1];
    int tmp=min(rem,e[2]);
    rem-=tmp;e[2]-=tmp;
    e[0]-=rem;e[1]-=rem;
    if(e[0]<0||e[1]<0)return 0;
    rem=c[2];
    tmp=min(rem,e[1]);
    rem-=tmp;e[1]-=tmp;
    e[0]-=rem;e[2]-=rem;
    if(e[0]<0||e[2]<0)return 0;
    rem=c[3];
    tmp=min(rem,e[0]);
    rem-=tmp;e[0]-=tmp;
    e[2]-=rem;e[1]-=rem;
    if(e[2]<0||e[1]<0)return 0;
    int mv=max(e[0],max(e[1],e[2]));
    int sum=e[0]+e[1]+e[2];
    int can=min(sum-mv,sum/2);
    if(can<c[4])return 0;
    return 1;
}
char ko[5];
int ty[110000];
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b),a){
        scanf("%s%s%s",A,B,C);
        for(int i=0;i<5;i++)c[i]=0;
        for(int i=0;i<a;i++){
            if(A[i]==B[i]&&B[i]==C[i])ty[i]=0;
            else if(A[i]==B[i])ty[i]=1;
            else if(A[i]==C[i])ty[i]=2;
            else if(C[i]==B[i])ty[i]=3;
            else ty[i]=4;
            c[ty[i]]++;
        }
    //  for(int i=0;i<5;i++)printf("%d ",c[i]);
        for(int i=0;i<3;i++)d[i]=b;
        if(!chk()){
            printf("-1\n");continue;
        }
        ret[a]=0;
        for(int i=0;i<a;i++){
            c[ty[i]]--;
            ko[0]='A';ko[1]=A[i];ko[2]=B[i];ko[3]=C[i];
            std::sort(ko,ko+4);
            for(int j=0;j<4;j++){
                if(A[i]!=ko[j])d[0]--;
                if(B[i]!=ko[j])d[1]--;
                if(C[i]!=ko[j])d[2]--;
                if(chk()){ret[i]=ko[j];break;}
                if(A[i]!=ko[j])d[0]++;
                if(B[i]!=ko[j])d[1]++;
                if(C[i]!=ko[j])d[2]++;
            }
        }
        printf("%s\n",ret);
    }
}
accuracy: 1
time_ms: 40
memory_kb: 1808
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
#include <complex>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
string A,B,C;
int type(int i){
	if(A[i]==B[i]&&B[i]==C[i]) return 4;
	if(B[i]==C[i]) return 0;
	if(C[i]==A[i]) return 1;
	if(A[i]==B[i]) return 2;
	return 3;
}
int main(){
	while(true){
		int N,d;
		cin>>N>>d;
		if(N==0) break;
		cin>>A>>B>>C;
		int a=d,b=d,c=d;
		int t[5]={};
		string ans;
		rep(i,N) t[type(i)]++;
		rep(i,N){
			vector<char> cand;
			cand.pb(A[i]);
			cand.pb(B[i]);
			cand.pb(C[i]);
			cand.pb('A');
			sort(all(cand));
			t[type(i)]--;
			for(char ch:cand){
				int l[3]={a,b,c};
				if(ch!=A[i]) l[0]--;
				if(ch!=B[i]) l[1]--;
				if(ch!=C[i]) l[2]--;
				bool hoge=true;
				rep(j,3) if(l[j]<0) hoge=false;
				if(!hoge) continue;
				int nt[5];
				rep(j,5) nt[j]=t[j];
				rep(j,3){
					if(l[j]<nt[j]) nt[j]-=l[j],l[j]=0;
					else l[j]-=nt[j],nt[j]=0;
				}
				l[0]-=nt[1]+nt[2];
				l[1]-=nt[2]+nt[0];
				l[2]-=nt[0]+nt[1];
				bool can=true;
				rep(j,3) if(l[j]<0) can=0;
				if(l[0]+l[1]+l[2]<2*nt[3]) can=0;
				int h=0;
				rep(j,3) h+=max(0,l[j]-nt[3]);
				if(h>l[0]+l[1]+l[2]-2*nt[3]) can=0;
				if(can){
					ans+=ch;
					if(ch!=A[i]) a--;
					if(ch!=B[i]) b--;
					if(ch!=C[i]) c--;
					break;
				}
			}
			if(ans.size()==0){
				ans="-1";
				break;
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 270
memory_kb: 1760
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
char A[110000];
char B[110000];
char C[110000];
char ret[110000];
int c[5];
int d[3];
int e[3];
int chk(){
	for(int i=0;i<3;i++)e[i]=d[i];
	int rem=c[1];
	int tmp=min(rem,e[2]);
	rem-=tmp;e[2]-=tmp;
	e[0]-=rem;e[1]-=rem;
	if(e[0]<0||e[1]<0)return 0;
	rem=c[2];
	tmp=min(rem,e[1]);
	rem-=tmp;e[1]-=tmp;
	e[0]-=rem;e[2]-=rem;
	if(e[0]<0||e[2]<0)return 0;
	rem=c[3];
	tmp=min(rem,e[0]);
	rem-=tmp;e[0]-=tmp;
	e[2]-=rem;e[1]-=rem;
	if(e[2]<0||e[1]<0)return 0;
	int mv=max(e[0],max(e[1],e[2]));
	int sum=e[0]+e[1]+e[2];
	int can=min(sum-mv,sum/2);
	if(can<c[4])return 0;
	return 1;
}
char ko[5];
int ty[110000];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		scanf("%s%s%s",A,B,C);
		for(int i=0;i<5;i++)c[i]=0;
		for(int i=0;i<a;i++){
			if(A[i]==B[i]&&B[i]==C[i])ty[i]=0;
			else if(A[i]==B[i])ty[i]=1;
			else if(A[i]==C[i])ty[i]=2;
			else if(C[i]==B[i])ty[i]=3;
			else ty[i]=4;
			c[ty[i]]++;
		}
	//	for(int i=0;i<5;i++)printf("%d ",c[i]);
		for(int i=0;i<3;i++)d[i]=b;
		if(!chk()){
			printf("-1\n");continue;
		}
		ret[a]=0;
		for(int i=0;i<a;i++){
			c[ty[i]]--;
			ko[0]='A';ko[1]=A[i];ko[2]=B[i];ko[3]=C[i];
			std::sort(ko,ko+4);
			for(int j=0;j<4;j++){
				if(A[i]!=ko[j])d[0]--;
				if(B[i]!=ko[j])d[1]--;
				if(C[i]!=ko[j])d[2]--;
				if(chk()){ret[i]=ko[j];break;}
				if(A[i]!=ko[j])d[0]++;
				if(B[i]!=ko[j])d[1]++;
				if(C[i]!=ko[j])d[2]++;
			}
		}
		printf("%s\n",ret);
	}
}
accuracy: 1
time_ms: 50
memory_kb: 1812
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool solve() {
    int L, D;
    if(!(cin >> L >> D)) return false;
    if(!L && !D) return false;

    string strs[3];
    for(int i = 0; i < 3; ++i) {
        cin >> strs[i];
    }

    vector<int> diff_by_one[3];
    vector<int> all_diff(L+1, 0);
    for(int i = 0; i < 3; ++i) {
        diff_by_one[i].resize(L+1, 0);
        const int next = (i+1) % 3;
        const int prev = (i+2) % 3;
        for(int j = 0; j < L; ++j) {
            if(strs[i][j] != strs[next][j] && strs[next][j] == strs[prev][j]) {
                diff_by_one[i][j] = 1;
            }
        }
        for(int j = L-1; j >= 0; --j) {
            diff_by_one[i][j] += diff_by_one[i][j+1];
        }
    }
    for(int i = 0; i < L; ++i) {
        if(strs[0][i] != strs[1][i] && strs[1][i] != strs[2][i] && strs[2][i] != strs[0][i]) {
            all_diff[i] = 1;
        }
    }
    for(int i = L-1; i >= 0; --i) {
        all_diff[i] += all_diff[i+1];
    }

    string res;
    vector<int> remain(3, D);
    for(int i = 0; i < L; ++i) {
        char ch = '\x7f';
        for(int idx = 0; idx < 4; ++idx) {
            const char test_ch = idx == 0 ? 'A' : strs[idx-1][i];
            int sum = 0;
            int r[3] = {0};
            int u[3] = {0};
            for(int j = 0; j < 3; ++j) {
                const int next_rem = remain[j] - (test_ch == strs[j][i] ? 0 : 1);
                r[j] = next_rem - diff_by_one[j][i+1];
                u[j] = all_diff[i+1] - r[j];
                sum += max(0, u[j]);
            }
            bool ok = sum <= all_diff[i+1];
            if(!ok) {
                if(u[0] <= 0 && u[1] <= 0) {
                    ok = sum - min({diff_by_one[2][i+1], -u[0], -u[1]}) <= all_diff[i+1];
                }
                if(u[1] <= 0 && u[2] <= 0) {
                    ok = sum - min({diff_by_one[0][i+1], -u[1], -u[2]}) <= all_diff[i+1];
                }
                if(u[2] <= 0 && u[0] <= 0) {
                    ok = sum - min({diff_by_one[1][i+1], -u[2], -u[0]}) <= all_diff[i+1];
                }
            }
            if(ok) {
                ch = min(ch, test_ch);
                //cout << i << ' ' << ch << endl;
            }
            if(ch == 'A') break;
        }
        if(ch == '\x7f') {
            res = "-1";
            break;
        } else {
            res += ch;
            //cout << res << endl;
            for(int j = 0; j < 3; ++j) {
                remain[j] -= (ch == strs[j][i] ? 0 : 1);
            }
        }
    }
    cout << res << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3392
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;


char str[3][100001];
char ans[100001];

bool check(int pair[3], int dist, int same, int dif[3]) {
  // cout << "pair : "; REP(i,3) cout << pair[i] << ","; cout << endl;
  // cout << "dist : " << dist << endl;
  // cout << "same : " << same << endl;
  // cout << "dif : "; REP(i,3) cout << dif[i] << ","; cout << endl;
  int u[3];
  int sum = 0;
  REP(i,3) {
    // dist-x<=dif[i]-pair[i]
    // x >= dist  - (dif[i]-pair[i])
    u[i] = max(0, dist-(dif[i]-pair[i]));
    sum += u[i];
  }
  if (sum<=dist) return 1;
  REP(i,3) {
    int j = (i+1)%3;
    int k = (i+2)%3;
    if (u[j]==0 && u[k]==0) {
      int r = min(pair[i], min(dif[j]-pair[j]-dist, dif[k]-pair[k]-dist));
      if (sum-r <= dist) return 1;
    }
  }
  return 0;
}

int main() {
  int l,d;
  while(cin >> l >> d, l||d) {
    int pair[3] = {};
    int dist = 0;
    int same = 0;
    REP(i,3) scanf("%s",str[i]);
    REP(i,l) {
      char c[3] = {str[0][i],str[1][i],str[2][i]};
      if (c[0]==c[1]&&c[1]==c[2]) {
        same++;
      } else {
        bool ok = 0;
        REP(j,3) {
          if (c[(j+1)%3]==c[(j+2)%3]) {
            ok = 1;
            pair[j]++;
          }
        }
        if (!ok) {
          dist++;
        }
      }
    }

    bool ok = 0;
    int dif[3] = {d,d,d};
    REP(i,l) {
      char c[3] = {str[0][i],str[1][i],str[2][i]};
      char cand[4] = {'A',c[0],c[1],c[2]};
      sort(cand,cand+4);
      
      if (c[0]==c[1]&&c[1]==c[2]) {
        same--;
      } else {
        bool ok = 0;
        REP(j,3) {
          if (c[(j+1)%3]==c[(j+2)%3]) {
            ok = 1;
            pair[j]--;
          }
        }
        if (!ok) {
          dist--;
        }
      }

      ok = 0;
      REP(j,4) {
        REP(k,3) {
          if (c[k]!=cand[j]) {
            dif[k]--;
          }
        }

        if (check(pair,dist,same,dif)) {
          ans[i] = cand[j];
          ok = 1;
          break;
        }
        
        REP(k,3) {
          if (c[k]!=cand[j]) {
            dif[k]++;
          }
        }
        
      }
      if (!ok) break;
    }
    if (!ok) cout << -1 << endl;
    else {
      ans[l] = 0;
      cout << ans << endl;
    }
  }
}
accuracy: 1
time_ms: 50
memory_kb: 1584
final_rank: 1