Omkar and Bed Wars
Contest: Codeforces Global Round 10
Difficulty rating: 1700
Tags: dp, greedy

Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $$$n$$$ players arranged in a circle, so that for all $$$j$$$ such that $$$2 \leq j \leq n$$$, player $$$j - 1$$$ is to the left of the player $$$j$$$, and player $$$j$$$ is to the right of player $$$j - 1$$$. Additionally, player $$$n$$$ is to the left of player $$$1$$$, and player $$$1$$$ is to the right of player $$$n$$$.

Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $$$0$$$, $$$1$$$, or $$$2$$$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $$$1$$$ other player, then they should logically attack that player in response. If instead a player is being attacked by $$$0$$$ or $$$2$$$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.

Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $$$n$$$ players in the game to make them instead attack another player  — i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left.

Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The descriptions of the test cases follows.

The first line of each test case contains one integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$)  — the amount of players (and therefore beds) in this game of Bed Wars.

The second line of each test case contains a string $$$s$$$ of length $$$n$$$. The $$$j$$$-th character of $$$s$$$ is equal to L if the $$$j$$$-th player is attacking the player to their left, and R if the $$$j$$$-th player is attacking the player to their right.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy.

It can be proven that it is always possible for Omkar to achieve this under the given constraints.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, players $$$1$$$ and $$$2$$$ are attacking each other, and players $$$3$$$ and $$$4$$$ are attacking each other. Each player is being attacked by exactly $$$1$$$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $$$0$$$.

In the second test case, not every player acts logically: for example, player $$$3$$$ is attacked only by player $$$2$$$, but doesn't attack him in response. Omkar can talk to player $$$3$$$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $$$1$$$.

Example input:
5
4
RLRL
6
LRRRRL
8
RLLRRRLL
12
LLLLRRLRRRLL
5
RRRRR

Example output:
0
1
1
3
2

Editorial:
As described in the statement, the only situation in which a player is not acting logically according to Bed Wars strategy is when they are being attacked by exactly $$$1$$$ player, but they are not attacking that player in response. Let the player acting illogically be player $$$j$$$. There are two cases in which player $$$j$$$ is acting illogically:
The first case is that player $$$j$$$ is being attacked by the player to their left, player $$$j - 1$$$, and not by the player to their right, player $$$j + 1$$$. This means that $$$s_{j - 1} = $$$ R, as they are attacking player $$$j$$$ who is to their right, and $$$s_{j + 1} = $$$ R, as they are attacking the player to the right instead of player $$$j$$$ who is to their left. Furthermore, since player $$$j$$$ is not being logical, instead of attacking player $$$j - 1$$$, they are attacking player $$$j + 1$$$, so $$$s_j = $$$ R. This means that in this case $$$s_{j - 1} = s_j = s_{j + 1} = $$$ R, i. e. R occurs $$$3$$$ times in a row somewhere in $$$s$$$. We want to avoid this case, so the final string (after Omkar has convinced some player to change) cannot have $$$3$$$ Rs in a row.
The second case is that player $$$j$$$ is being attacked by the player to their right, player $$$j + 1$$$, and not by the player to their left, player $$$j - 1$$$. It is easy to see that this case is essentially the same as the first case, but reversed, meaning that the final string also cannot have $$$3$$$ Ls in a row.
Combining these two cases, the condition in the statement reduces to the simple condition that the same character cannot occur $$$3$$$ times in a row in our final string.
If we have some subsegment of length $$$l$$$ of the same character in $$$s$$$, and this subsegment is maximal, so that the characters preceding and following it in $$$s$$$ are different from the characters in it, then we can make all players in this subsegment logical by having Omkar talk to $$$\lfloor \frac l 3 \rfloor$$$ of the players in that subsegment. Therefore, assuming that not all characters in $$$s$$$ are the same, we simply find the lengths of all maximal subsegments of the same characters in $$$s$$$ (noting that we need to wrap around if $$$s_1 = s_n$$$), and sum over their lengths divided (rounding down) by $$$3$$$. Finding these lengths and summing over their quotients can be done easily by looping through $$$s$$$ in $$$O(n)$$$.
If all the characters in $$$s$$$ are the same, then we can see similarly that Omkar needs to talk to $$$\lceil \frac n 3 \rceil$$$ of the players. (Note that we are rounding up instead of down  — this is due to the circular nature of $$$s$$$).