#5407. Problem 1. It's Mooin' Time IV

0

Problem 1. It's Mooin' Time IV

Problem 1. It's Mooin' Time IV

USACO 2026 Second Contest, Bronze

Bessie has a computer with a keyboard that only has two letters, M and O.

Bessie wants to type her favorite moo SS consisting of NN letters, each of which is either an M or an O. However, her computer has been hit with a virus. Every time she tries to type the letter O, every letter that she has typed so far flips, either from M to O or from O to M, before the O appears.

Is it possible for Bessie to type out her favorite moo?

Additionally, Bessie is given a parameter kk which is either 00 or 11.

  • If k=0k = 0, then Bessie only needs to determine whether it is possible to type out her favorite moo.
  • If k=1k = 1, then Bessie also needs to give an example of a sequence of keystrokes to type so she can type out her favorite moo.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains TT, the number of independent test cases (1T1041\le T\le 10^4) and kk (0k10 \le k \le 1).

The first line of each test case has NN (1N21051 \le N \le 2 \cdot 10^5).

The second line of each test case has SS. It is guaranteed that no characters will appear in SS besides M\texttt{M} and O\texttt{O}.

The sum of NN across all test cases will not exceed 41054 \cdot 10^5.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output either one or two lines using the following procedure.

If it is impossible for Bessie to type out SS, print NO\texttt{NO} on a single line.

Otherwise, on the first line print YES\texttt{YES}. Furthermore, if k=1k=1, on the second line, print a string of length NN, the characters in order that Bessie needs to type in order to type out her favorite moo. If there are multiple such strings, any will be accepted.

SAMPLE INPUT:


2 0
3
MOO
5
OOMOO

SAMPLE OUTPUT:


YES
YES

SAMPLE INPUT:


2 1
3
MOO
5
OOMOO

SAMPLE OUTPUT:


YES
OMO
YES
MOOMO

As Bessie types out MOOMO\texttt{MOOMO}, this is how the letters change:

  1. Before typing the first M\texttt{M}, Bessie has an empty string. Afterwards, she has the string M\texttt{M}.
  2. After typing the first O\texttt{O}, the M\texttt{M} flips to O\texttt{O}, and then the O\texttt{O} is appended to form OO\texttt{OO}.
  3. After typing the second O\texttt{O}, the OO\texttt{OO} flips to MM\texttt{MM}, and then the O\texttt{O} is appended to form MMO\texttt{MMO}.
  4. After typing the second M\texttt{M}, Bessie has the string MMOM\texttt{MMOM}.
  5. After typing the last O\texttt{O}, the string MMOM\texttt{MMOM} flips to OOMO\texttt{OOMO}, and then the O\texttt{O} is appended to form OOMOO\texttt{OOMOO}, as desired.

SCORING: Inputs 3-4: k=0k=0. Inputs 5-6: k=1,T103,N10k=1, T \le 10^3, N \le 10. Inputs 7-9: k=1,T10,N1000k=1, T \le 10, N \le 1000. Inputs 10-16: k=1k=1.

Problem credits: Nick Wu