题面
A string consisting of lowercase Latin letters is called suspicious if and only if all of the following conditions hold:
- The letter appears at least twice, and
- For every occurrence of the letter , the two nearest occurrences of are the same number of characters away from the .
After watching you finish a string task, your friend Aka has gifted you a string consisting only of letters and . You can perform the following operation on :
- Choose an index (), and set to .
Determine the minimum number of operations needed to make suspicious. It can be shown that, under the given constraints, it is always possible to transform into a suspicious string.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The only line of each test case contains the string (). It is guaranteed that or .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimum number of operations needed to make suspicious.
思路
将目标串记为 且只含 操作等价于选择一些位置把
若 或 则最近的两次 只能在同一侧,而到端点的距离必不相等,因此首尾必须为
对任意位置 的 设其左右最近的 与它的距离同为 则这两个 必在 与
若 则 也是 且它到两侧最近 的距离分别为 与 不相等,矛盾
故必有 ,即每个 都被形如 包住 等价于 不包含子串
综上,满足:suspicious 的充要条件为:首尾为 且不存在相邻的