CF BUDDY
← Problems·

1984D · "a" String Problem

2000 · brute force, hashing, implementation

Problem: You are given a string ss consisting of lowercase Latin characters. Count the number of nonempty strings tt \neq "a\texttt{a}" such that it is possible to partition^{\dagger} ss into some substrings satisfying the following conditions:

  • each substring either equals tt or "a\texttt{a}", and
  • at least one substring equals tt.

^{\dagger} A partition of a string ss is an ordered sequence of some kk strings t1,t2,,tkt_1, t_2, \ldots, t_k (called substrings) such that t1+t2++tk=st_1 + t_2 + \ldots + t_k = s, where ++ represents the concatenation operation.

Input Format: The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

The only line of each test case contains a string ss consisting of lowercase Latin characters (2s21052 \leq |s| \leq 2 \cdot 10^5).

The sum of s|s| over all test cases does not exceed 31053 \cdot 10^5.

Output Format: For each test case, output a single integer — the number of nonempty strings tt \neq "a\texttt{a}" that satisfy all constraints.

Note: In the first test case, tt can be "aa\texttt{aa}", "aaa\texttt{aaa}", "aaaa\texttt{aaaa}", or the full string.

In the second test case, tt can be "b\texttt{b}", "bab\texttt{bab}", "ba\texttt{ba}", or the full string.

In the third test case, the only such tt is the full string.

Sample Cases

Case 1

Input

8
aaaaa
baba
cabacb
aaabaaa
bitset
ab
abbaaaabbb
yearnineteeneightyfour

Output

4
4
1
16
1
2
3
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback