CANDYSTRIPE
We have chocolates arranged in a row, each wrapped and placed in a film. Some of these chocolates are bitter ("") and some are sweet (""). For example, the string "" represents a film consisting of chocolates, where the first one is bitter, the second one is sweet, and so on.
Our two-year-old brother naturally prefers sweet chocolates. He won't complain if he eats any bitter chocolates as well. However, if at any moment he has eaten more bitter chocolates than sweet ones, he will burst into tears, and of course, we don't want to upset him.
We want to give our brother a contiguous segment of the original film, possibly by trimming from the beginning and/or the end. Our brother will start eating the chocolates one by one in order until they are finished, but we don't know from which end of the segment he will start.
Find the length of the longest segment of the original film that we can give to our brother, ensuring that he won't burst into tears.
Input:
The first line of the input will contain a positive integer : the number of initial films for which you need to find the solution. Each of the next lines will correspond to an initial film and will contain a string consisting of characters "" and "".
Output:
The output should contain lines, each line containing exactly one integer: the answer to the above problem for the corresponding initial film.
Constraints:
- .
- The length of each film will be .
- The total length of all films in each test case will not exceed .
- Time Limit: sec.
- Memory Limit: MB.
- For test cases with a total value of , it will be .
- For test cases with a total value of , it will be .
Example of Input - Ouput:
input
5
BSBSSB
SSBBSS
SBSBSBBSSS
SSSBBSBSBSBBB
ΒΒΒΒ
output
4
6
5
5
0
Explanation of the example:
For the first film, the longest segment we can give to our brother is "", which results from cutting off the two bitter chocolates at both ends. For the second film, we can give him the entire film. For the third film, the longest segment we can give him is the prefix "". The same applies to the fourth film, where the segment "" results from cutting the first five and the last three chocolates. Finally, for the fifth film, there is no segment that we can give to our brother that would make him happy since there are no sweet chocolates.
Comments