308371: CF1509B. TMT Document

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

TMT Document

题意翻译

给定一个由字符 `T` 和字符 `M` 构成的、长度为 $n$ 的字符串 $s$,你要把它划分成若干个不相交的子序列,使得所有子序列都等于 `TMT`。判断是否有解,有解输出 `YES`,无解输出 `NO`。$T$ 组数据。 $1\leq T\leq5\times10^3;3\leq n<10^5;\sum n\leq10^5;$ 保证字符串 $s$ 仅由 `T` 和 `M` 构成,保证 $3|n$。

题目描述

The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it. However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length $ n $ whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences. A string $ a $ is a subsequence of a string $ b $ if $ a $ can be obtained from $ b $ by deletion of several (possibly, zero) characters.

输入输出格式

输入格式


The first line contains an integer $ t $ ( $ 1 \le t \le 5000 $ ) — the number of test cases. The first line of each test case contains an integer $ n $ ( $ 3 \le n < 10^5 $ ), the number of characters in the string entered in the document. It is guaranteed that $ n $ is divisible by $ 3 $ . The second line of each test case contains a string of length $ n $ consisting of only the characters T and M. It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

输出格式


For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise.

输入输出样例

输入样例 #1

5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT

输出样例 #1

YES
NO
YES
NO
YES

说明

In the first test case, the string itself is already a sequence equal to TMT. In the third test case, we may partition the string into the subsequences TMTMTT. Both the bolded and the non-bolded subsequences are equal to TMT.

Input

题意翻译

给定一个由字符 `T` 和字符 `M` 构成的、长度为 $n$ 的字符串 $s$,你要把它划分成若干个不相交的子序列,使得所有子序列都等于 `TMT`。判断是否有解,有解输出 `YES`,无解输出 `NO`。$T$ 组数据。 $1\leq T\leq5\times10^3;3\leq n<10^5;\sum n\leq10^5;$ 保证字符串 $s$ 仅由 `T` 和 `M` 构成,保证 $3|n$。

加入题单

算法标签: