33OJ
登录 注册

#P14225. [ICPC 2024 Kunming I] 左移 2

[ICPC 2024 Kunming I] 左移 2

Problem Description

Given a string consisting of lowercase letters, we say the string is beautiful if every pair of adjacent characters in the string are different. For example, icpc\texttt{icpc} and kunming\texttt{kunming} are beautiful, but hello\texttt{hello} is not, because its 33-rd and 44-th characters are the same.

Given a string S=s0s1⋯sn−1S = s_0s_1\cdots s_{n-1} of length nn consisting of lowercase English letters, let f(S,d)f(S, d) denote the string obtained by shifting SS left by dd positions. That is, $f(S, d) = s_{(d+0)\bmod n}s_{(d+1)\bmod n}\cdots s_{(d+n-1)\bmod n}$.

Let g(S,d)g(S, d) denote the minimum number of operations needed to make f(S,d)f(S, d) beautiful. In each operation, you may change any single character of f(S,d)f(S, d) into any lowercase letter.

Find a non-negative integer dd that minimizes g(S,d)g(S, d), and output this minimum value.

Input Format

There are multiple test cases. The first line contains an integer TT, the number of test cases. For each test case:

The first line contains a string s0s1⋯sn−1s_0s_1\cdots s_{n-1} consisting only of lowercase letters (1≤n≤5×1051 \le n \le 5 \times 10^5).

It is guaranteed that the sum of nn over all test cases does not exceed 5×1055 \times 10^5.

Output Format

For each test case, output one line with one integer, the minimum value of g(S,d)g(S, d).

3
abccbbbbd
abcde
x
2
0
0

Hint

For the first sample case, consider d=5d = 5. We have f(S,5)=f(S, 5) = bbbdabccb\texttt{bbbdabccb}. For this string, we can change its 22-nd character into x\texttt{x}, and change its 88-th character into y\texttt{y}. Then the string becomes bxbdabcyb\texttt{bxbdabcyb}, which is a beautiful string.

Translated by QwenWork