No.3374 Caesar Shift Game
タグ : / 解いたユーザー数 51
作問者 : 👑
Iroha_3856
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 withB. - If the $i$-th character of $S$ is
B, replace it withC. - If the $i$-th character of $S$ is
C, replace it withA.
- 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$.
Cis replaced withA. - Bob chooses $i = 2$.
Bis replaced withC. - Alice declares the end of the game.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。