問題一覧 > 通常問題

No.3374 Caesar Shift Game

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 51
作問者 : 👑 loop0919 / テスター : lif4635 Iroha_3856
お気に入りにしたユーザー ProblemId : 12866 / 自分の提出
問題文最終更新日: 2025-11-21 19:46:42
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 489 (順位表) の他の問題:

Problem Statement

You are given a string $S$ of length $N$ consisting of A, B, C.

Alice and Bob play the following game using $S$.

Alice moves first and Bob moves second, and they alternately repeat choosing and performing one of the following operations.

  • Choose one integer $i$ between $1$ and $N$, inclusive, that the opponent has never chosen, and perform the following replacement.
    • If the $i$-th character of $S$ is A, replace it with B.
    • If the $i$-th character of $S$ is B, replace it with C.
    • If the $i$-th character of $S$ is C, replace it with A.
  • Declare the end of the game.

The game ends as soon as either player declares the end, or both players have each performed $10^{100}$ operations.

Alice's goal is to make $S$ at the end of the game as lexicographically small as possible.
Conversely, Bob's goal is to make $S$ at the end of the game as lexicographically large as possible.

Find $S$ at the end of the game when both players play optimally$\color{red}^{*1}$.

You are given $T$ test cases; answer each of them.

$\color{red}^{*1}$ What it means for both players to play optimally (click to expand)

It can be shown that there exists exactly one string $X$ of length $N$ consisting of A, B, C that satisfies all of the following.

  • No matter how Bob operates, Alice can make $S$ at the end of the game lexicographically less than or equal to $X$ by playing the game appropriately.
  • No matter how Alice operates, Bob can make $S$ at the end of the game lexicographically greater than or equal to $X$ by playing the game appropriately.

This $X$ is defined to be $S$ at the end of the game when both players play optimally.

Constraints

  • $1 \leq T \leq 10^4$
  • $T$ is an integer.
  • Each test case satisfies all of the following constraints.
    • $1 \leq N \leq 5 \times 10^5$
    • $N$ is an integer.
    • $S$ is a string of length $N$ consisting of A, B, C.
  • The sum of $N$ in a single input file is at most $5 \times 10^5$.

Input

The input is given from Standard Input in the following format. Here, $\mathrm{case}_t ~ (1 \leq t \leq T)$ denotes the $t$-th test case.

$T$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_T$

Each test case is given in the following format.

$N$
$S$

Output

Print $T$ lines. The $t$-th line should contain the answer for the $t$-th test case.

Sample

Sample 1
Input
2
3
ABC
5
CBABC
Output
ABC
ACABC

For the $1$-st test case, it is optimal for Alice to declare the end at the very beginning.

For the $2$-nd test case, when both players play optimally, the operations proceed as follows and $S$ becomes ACABC.

  • Alice chooses $i = 1$. C is replaced with A.
  • Bob chooses $i = 2$. B is replaced with C.
  • Alice declares the end of the game.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。