// Implement 48min // debug1 15min // debug2 45min // debug3 21min #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) enum { S_ALL, D_ALL, S_01, S_12, S_20, }; int len; const int n = 3; char str[3][1000010]; char candidate[1000010][4]; char state[1000010]; char ans[1000010]; int rest[3]; const int INF = 1 << 29; bool Solve(vector cnt, int index, char c) { cnt[(int)state[index]]--; int diff = cnt[D_ALL]; int x = rest[0]; int y = rest[1]; int z = rest[2]; if (c != str[0][index]) { x--; } if (c != str[1][index]) { y--; } if (c != str[2][index]) { z--; } x -= cnt[S_12]; y -= cnt[S_20]; z -= cnt[S_01]; int ux = max(0, diff - x); int uy = max(0, diff - y); int uz = max(0, diff - z); if (ux + uy <= z && uy + uz <= x && uz + ux <= y) { return true; } int target = 0; if (uy + uz > x) { target |= 1 << 0; } if (uz + ux > y) { target |= 1 << 1; } if (ux + uy > z) { target |= 1 << 2; } if (__builtin_popcount(target) > 1) { return false; } assert(__builtin_popcount(target) == 1); int nx = (uy + uz) - x; int ny = (uz + ux) - y; int nz = (ux + uy) - z; if (target == 1) { ny = nz = 0; x += INF; } if (target == 2) { nz = nx = 0; y += INF; } if (target == 4) { nx = ny = 0; z += INF; } if (nx <= cnt[S_12] && ny <= cnt[S_20] && nz <= cnt[S_01] && ux + uy <= z && uy + uz <= x && uz + ux <= y) { return true; } return false; } void Sub(vector &cnt, int index, char c) { REP(i, n) { if (str[i][index] != c) { rest[i]--; } } cnt[(int)state[index]]--; } int main() { int d; while (scanf("%d %d", &len, &d) > 0 && (len > 0 || d > 0)) { vector cnt(5, 0); // Init & Input REP(i, n) { scanf("%s", str[i]); } ans[len] = '\0'; // Kernelize REP(i, len) { REP(j, n) { candidate[i][j] = str[j][i]; } candidate[i][n] = 'A'; sort(candidate[i], candidate[i] + n + 1); if (str[0][i] == str[1][i] && str[0][i] == str[2][i]) { state[i] = S_ALL; } else if (str[0][i] == str[1][i]) { state[i] = S_01; } else if (str[1][i] == str[2][i]) { state[i] = S_12; } else if (str[2][i] == str[0][i]) { state[i] = S_20; } else { state[i] = D_ALL; } cnt[(int)state[i]]++; } // Solve rest[0] = rest[1] = rest[2] = d; if (!Solve(cnt, 0, str[0][0]) && !Solve(cnt, 0, str[1][0]) && !Solve(cnt, 0, str[2][0])) { puts("-1"); continue; } REP(i, len) { REP(j, n + 1) { if (Solve(cnt, i, candidate[i][j])) { ans[i] = candidate[i][j]; Sub(cnt, i, candidate[i][j]); break; } assert(j != n); } } //puts("1"); puts(ans); } }