#P9574. Maximizing the T‐Block Weight
Maximizing the T‐Block Weight
Maximizing the T‐Block Weight
You are given a string s of length n consisting only of the characters B
and T
. You are allowed to perform the following operation any number of times (possibly zero):
Choose a substring of length 4 that is exactly BTTB
and change it to TBBT
.
A T‐segment (or contiguous block of T’s) of a string is any substring consisting only of the character T
. The weight of a string is defined as the length of its longest T‐segment.
Your task is to use the allowed operation in order to make the string’s weight as large as possible, and then output that maximum weight.
Invariants and Notes
- The only allowed operation is replacing a substring exactly equal to
\(\tt BTTB\) with \(\tt TBBT\). Note that this operation does not change the total count of
T
(orB
) in the string. - It can be verified that both the total number of
T
's and the sum of positions (indices are 1-indexed) whereT
appears remains invariant under this operation. In other words, if we denote \(k\) as the total number ofT
's and \(S\) as the sum of their positions, then no matter how you apply the operations you will always have the same \(k\) and \(S\). - The problem thus reduces to: given that the positions of
T
's (in increasing order) are a sequence of \(k\) numbers with fixed sum \(S\), what is the maximum integer \(r\) (with \(1\le r\le k\)) such that it is possible to have a contiguous segment of \(r\) T’s (i.e. located in some positions \(p,p+1,\ldots,p+r-1\)) while still placing the remaining \(k-r\) T’s in the other positions (from \(1\) to \(n\) but outside \(\{p,\ldots,p+r-1\}\)) so that the overall sum of positions is \(S\)?
More formally, if you decide that a contiguous block of T’s will occupy positions \(p, p+1, \ldots, p+r-1\) (with \(1 \le p \le n-r+1\)), then the sum contributed by these T’s is \[ A = \frac{r(2p + r - 1)}{2}. \] The remaining \(k-r\) T’s must be placed in the available positions \(\{1,2,\ldots,p-1\} \cup \{p+r, p+r+1,\ldots,n\}\) in such a way that the sum of their positions equals \(S - A\). Your task is to choose \(r\) as large as possible for which there exists an integer \(p\) (and a valid placement for the remaining T’s) meeting the invariant.
Input Format
A single line containing a string s
of characters, each of which is either B
or T
.
Output Format
Output a single integer — the maximum weight (i.e. the maximum possible length of a contiguous segment of T’s) achievable by applying the allowed operations arbitrarily many times.
Example
Input: BTTB
Explanation: The string has two T’s initially (at positions 2 and 3) so that \(k=2, S=5\). Notice that a contiguous block of length 2 can appear at positions 2 and 3 (since \(2+3=5\)).
Output: 2
Another Example
Input: TBT
Here, \(k=2\) and \(S=1+3=4\). The possible contiguous blocks of 2 T’s have sums 3 (if placed at positions 1-2) or 5 (if placed at 2-3). Neither equals 4, so the best achievable weight is 1.
inputFormat
The input consists of a single line containing a string s
composed only of the characters B
and T
.
outputFormat
Output a single integer — the maximum contiguous T‐segment length (i.e. the weight) that can be obtained after performing the operations optimally.
sample
BTTB
2