#P4415. Ants Jumping Over Each Other

    ID: 17661 Type: Default 1000ms 256MiB

Ants Jumping Over Each Other

Ants Jumping Over Each Other

Ants are known to march in rows, with each ant (except the first) following another. When two rows of ants, moving in opposite directions, meet in a narrow passage, a fascinating phenomenon occurs: the ants jump over each other! Specifically, at the moment the rows meet, every second each ant attempts to jump over the ant immediately in front of it. This jump (i.e. a swap of positions) only happens if the ant in front is moving in the opposite direction.

Formally, if we label each ant with a character and attach a direction – with the left row ants moving to the right (denoted by R) and the right row ants moving to the left (denoted by L) – then in each second, for every adjacent pair (i, i+1) in the combined line, if

\( direction[i] = R \) and \( direction[i+1] = L \)

then these two ants will swap positions. Given the initial order of the ants and the time \( T \) seconds, determine the order of the ants after all jumps.

inputFormat

The input consists of four lines:

  1. The first line contains an integer \( T \) (\( 0 \le T \le 50 \)), the number of seconds.
  2. The second line contains two integers \( N_1 \) and \( N_2 \) (\( 1 \le N_1, N_2 \le 26 \)), representing the number of ants in the left row and the right row respectively.
  3. The third line contains a string of \( N_1 \) uppercase letters, representing the left-row ants in order from leftmost to rightmost. Note that since these ants are marching to the right, their order will be reversed when they meet.
  4. The fourth line contains a string of \( N_2 \) uppercase letters, representing the right-row ants in order from leftmost to rightmost (marching to the left).

outputFormat

Output a single string representing the order of the ants from leftmost to rightmost after \( T \) seconds.

sample

0
3 3
ABC
DEF
CBADEF