#P6709. [CCC 2020] Swapping Seats

[CCC 2020] Swapping Seats

Description

There are NN people sitting around a round table.

There are three groups, and each person belongs to one group.

Now you want people from the same group to sit together.

Each time, you may swap the seats of two people. Output the minimum number of swaps.

Input Format

A single line containing a string ss of length NN, representing the groups of all people in clockwise order.

If sis_i is A, then the ii-th person belongs to the first group, and so on.

Output Format

A single line containing one integer, the minimum number of swaps.

BABCBCACCA
2

Hint

Sample Explanation

$\texttt{BABCBCACCA}\to\texttt{AABCBCBCCA}\to\texttt{AABBBCCCCA}$.

Subtasks

This problem uses bundled testdata, and the Subtask scores have been slightly adjusted.

  • Subtask 1 (2626 points): si{s_i\in\{A,,B}\} and N5×103N\le 5\times 10^3.
  • Subtask 2 (2727 points): si{s_i\in\{A,,B}\}.
  • Subtask 3 (2727 points): N5×103N\le 5\times 10^3.
  • Subtask 4 (2020 points): No special constraints.

For 100%100\% of the testdata, it is guaranteed that si{s_i\in\{A,,B,,C}\} and 1N1061\le N\le 10^6.

Notes

This problem is translated from Canadian Computing Competition 2020 Senior T4 Swapping Seats.

Translated by ChatGPT 5