#P10244. String Minimization

String Minimization

Problem Description

You are given four strings a,b,c,da, b, c, d, each of length nn. You may perform the following operation any number of times:

  • Choose an index ii, swap aia_i and cic_i, then swap bib_i and did_i.

Find the lexicographically smallest possible string bb, under the condition that the resulting string aa is as lexicographically small as possible.


If you do not know what lexicographical order is, see below:

For two strings p,qp, q, we say pp is lexicographically smaller than qq (written as p<qp<q) if and only if there exists a non-negative integer kk such that the first kk characters of pp and qq are the same, and the ASCII code of pk+1p_{k+1} is smaller than that of qk+1q_{k+1}.

For example:

  • abc<baa\texttt{abc}<\texttt{baa} (when k=0k=0)
  • bae<bbb\texttt{bae}<\texttt{bbb} (when k=1k=1).

Input Format

The first line contains a positive integer nn, the length of strings a,b,c,da, b, c, d.

The next four lines each contain a string, representing a,b,c,da, b, c, d respectively.

Output Format

Output one line containing a string, the required string bb.

8
westlake
yummyqaq
weabzzke
azazazaq

auazyqaq

Hint

[Sample Explanation]

Choosing ii as 1,3,41, 3, 4 can make aa reach the minimum lexicographical order weablake\texttt{weablake}. At this time, string bb also becomes the minimum lexicographical order that satisfies the requirement, which is auazyqaq\texttt{auazyqaq}.

In fact, if we do not operate when i=1i=1, the lexicographical order of aa is still minimal, but then string bb would be yuazyqaq\texttt{yuazyqaq}, which is not small enough.

[Constraints]

This problem has 1010 test points, each worth 1010 points.

Test Point ID nn\le Special Property
121\sim 2 1515
33 10510^5 ai>cia_i>c_i
454\sim 5 aicia_i\ne c_i
676\sim 7 bidib_i\ge d_i
8108\sim 10

For all testdata, it is guaranteed that 1n1051\le n\le 10^5, and all characters in the strings are lowercase letters.

Translated by ChatGPT 5