383 字
2 分钟
Impost-or-Sus

题面#

A string ww consisting of lowercase Latin letters is called suspicious if and only if all of the following conditions hold:

  • The letter s\mathtt{s} appears at least twice, and
  • For every occurrence of the letter u\mathtt{u}, the two nearest occurrences of s\mathtt{s} are the same number of characters away from the u\mathtt{u}.

After watching you finish a string task, your friend Aka has gifted you a string rr consisting only of letters s\mathtt{s} and u\mathtt{u}. You can perform the following operation on rr:

  • Choose an index ii (1≤i≤∣r∣1\le i\le |r|), and set rir_i to s\mathtt{s}.

Determine the minimum number of operations needed to make rr suspicious. It can be shown that, under the given constraints, it is always possible to transform rr into a suspicious string.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The only line of each test case contains the string rr (3≤∣r∣≤2⋅1053\le |r|\le 2\cdot 10^5). It is guaranteed that ri=sr_i = \mathtt{s} or u\mathtt{u}.

It is guaranteed that the sum of ∣r∣|r| over all test cases does not exceed 2⋅1052\cdot 10 ^ 5.

Output

For each test case, output a single integer — the minimum number of operations needed to make rr suspicious.

思路#

将目标串记为 ww 且只含 s,u\mathtt{s},\mathtt{u} 操作等价于选择一些位置把 u→s\mathtt{u}\to\mathtt{s}

若 w1=uw_1=\mathtt{u} 或 w∣w∣=uw_{|w|}=\mathtt{u} 则最近的两次 s\mathtt{s} 只能在同一侧,而到端点的距离必不相等,因此首尾必须为 s\mathtt{s}

对任意位置 ii 的 u\mathtt{u} 设其左右最近的 s\mathtt{s} 与它的距离同为 dd 则这两个 s\mathtt{s} 必在 i−di-d 与 i+di+d

若 d>1d>1 则 i+1i+1 也是 u\mathtt{u} 且它到两侧最近 s\mathtt{s} 的距离分别为 d−1d-1 与 d+1d+1 不相等,矛盾

故必有 d=1d=1 ,即每个 u\mathtt{u} 都被形如 sus\mathtt{sus} 包住 等价于 ww 不包含子串 uu\mathtt{uu}

综上,满足:suspicious 的充要条件为:首尾为 s\mathtt{s} 且不存在相邻的 u\mathtt{u}

Impost-or-Sus
https://github.com/posts/算法/impost-or-sus/
作者
FZSGBall
发布于
2026-05-18
许可协议
CC BY-NC-SA 4.0