#P14021. [ICPC 2024 Nanjing R] 博德之跃 2

[ICPC 2024 Nanjing R] 博德之跃 2

Problem Description

You are given a string SS consisting of lower-cased English letters. You need to perform some operations on SS until it becomes empty. Each time you can perform one of the following three operations:

  • Delete the first character of SS.
  • Delete the last character of SS.
  • Choose a good substring S′S' of SS and replace SS with S′S'.

A non-empty string S′S' is called a good substring of string SS if and only if S′≠SS'\neq S, S′S' is a prefix of SS, and the reverse of S′S' is a suffix of SS. The reverse of a string p1p2⋯pkp_1p_2\cdots p_k of length kk is another string pkpk−1⋯p1p_kp_{k-1}\cdots p_1 of length kk.

What's the maximum number of type 33 operations can you perform?

Input Format

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

The first and only line contains a string SS (1≤∣S∣≤1051\le |S|\le 10^5) consisting of lower-cased English letters.

It is guaranteed that the sum of ∣S∣|S| over all test cases does not exceed 2×1052\times 10^5.

Output Format

For each test case, output one line containing one integer indicating the maximum number of type 33 operations you can perform.

3
aaaa
abbaabba
xy
3
4
0

Hint

For the first sample test case: $\texttt{aaaa} \xrightarrow{\text{op. 3}} \texttt{aaa} \xrightarrow{\text{op. 3}} \texttt{aa} \xrightarrow{\text{op. 3}} \texttt{a} \xrightarrow{\text{op. 2}} \varnothing$.

For the second sample test case: $\texttt{abbaabba} \xrightarrow{\text{op. 3}} \texttt{abbaabb} \xrightarrow{\text{op. 1}} \texttt{bbaabb} \xrightarrow{\text{op. 3}} \texttt{bbaab} \xrightarrow{\text{op. 1}} \texttt{baab} \xrightarrow{\text{op. 3}} \texttt{baa} \xrightarrow{\text{op. 1}} \texttt{aa} \xrightarrow{\text{op. 3}} \texttt{a} \xrightarrow{\text{op. 1}} \varnothing$.