#P9574. 「TAOI-2」Break Through the Barrier

    ID: 10125 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟贪心洛谷原创O2优化洛谷月赛

「TAOI-2」Break Through the Barrier

Problem Description

There is a string consisting of B\texttt{B} and T\texttt{T}.

You can perform the following operation: choose a substring of length 44 that is exactly BTTB\texttt{BTTB}, and change it to TBBT\texttt{TBBT}. You may perform this operation any number of times (or not perform it at all).

Define the weight of a string as the length of the longest consecutive segment of T\texttt{T}. You need to make the weight of the string as large as possible using the operation above. Output this maximum value.

  • Definition of substring: a string bb is called a substring of a string aa if and only if bb can be obtained by deleting some (possibly 00) characters from the beginning of aa and some characters from the end of aa.
  • Definition of a consecutive T\texttt{T} segment: a substring of the original string that consists only of T\texttt{T}.

Input Format

The first line contains a positive integer TT, indicating the number of test cases.

For each test case, the first line contains a positive integer nn, indicating the length of the string. The second line contains a string SS of length nn, which is the string in this problem.

Output Format

Output TT lines. Each line contains a non-negative integer, representing the length of the longest consecutive segment of T\texttt{T}.

6
3
TTT
4
BTTB
5
TBBTT
6
BTBTBB
7
BTTBTTB
17
TTBTBTTBTBTTTBTTB

3
2
2
1
3
5

Hint

This problem uses bundled testdata.

Let n\sum n be the sum of nn over all test cases.

  • Subtask 0 (5 pts): n7n \leq 7.
  • Subtask 1 (20 pts): T10T \leq 10, n10n \leq 10.
  • Subtask 2 (25 pts): n5000\sum n \leq 5000.
  • Subtask 3 (5 pts): it is guaranteed that the given string cannot perform any operation.
  • Subtask 4 (45 pts): no special restrictions.

For all testdata, 1T1031 \leq T \leq 10^3, 1n1051 \leq n \leq 10^5, 1n5×1051 \leq \sum n \leq 5 \times 10^5, and the string contains only the characters B and T.

Translated by ChatGPT 5