#P4405. Orez's Coin Merging Game
Orez's Coin Merging Game
Orez's Coin Merging Game
Orez loves games. Recently he invented a coin game defined on a circular table. First, the edge of the table is divided into \(2n\) positions, numbered \(1,2,\dots,2n\) in clockwise order. Then \(n\) coins are placed on the odd‐numbered positions. Each coin has two sides: heads and tails. The initial states of the coins (on positions \(1,3,5,\dots,2n-1\)) are given as a string of length \(n\) consisting of the characters 'H' (heads) and 'T' (tails).
After that, Orez performs the following operation exactly \(T\) times:
Choose any two coins that are consecutive in clockwise order. Suppose the two coins have states \(a\) and \(b\). Then, in the gap immediately after the first coin (that is, at position \((a_{pos} \bmod (2n)) + 1\) when the first coin is at position \(a_{pos}\)), place a new coin whose state is determined by its two neighbors according to the rule:
\( \text{new state} = \begin{cases} H, & \text{if } a = b, \\ T, & \text{if } a \neq b. \end{cases} \)
Then remove the two original coins from the table.
This operation is performed repeatedly. (Note: In our solution we fix a deterministic strategy: in each move, we choose the coin with the smallest position (when the coin positions are sorted in increasing order) and its clockwise neighbor. This completely determines the outcome.)
After \(T\) moves, there will be exactly \(n-T\) coins on the table. The final configuration is represented as a string of length \(2n\) where the character at position \(i\) is either 'H' or 'T' if a coin is present at that position, or a dot '.' if the position is empty. Positions are numbered from \(1\) to \(2n\) in clockwise order.
Your task is to simulate the process and output the final configuration.
inputFormat
The input consists of two lines:
- The first line contains two integers \(n\) and \(T\) (with \(1\le T < n\le 10^5\), for example), where \(n\) is the number of coins initially and \(T\) is the number of moves.
- The second line contains a string of length \(n\) consisting only of the characters 'H' and 'T', representing the initial states of the coins placed at positions \(1, 3, 5, \dots, 2n-1\) respectively.
outputFormat
Output a single line containing a string of length \(2n\). For each position from \(1\) to \(2n\), output its character: if a coin is present at that position, output its state ('H' or 'T'); otherwise output a dot '.'.
sample
3 1
HTH
.T..H.