1509B.md (1963B)
1 Source: https://codeforces.com/problemset/problem/1509/B 2 3 B. TMT Document 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters. 26 27 Input 28 29 The first line contains an integer $t$ ($1 \le t \le 5000$) — the number of test cases. 30 31 The first line of each test case contains an integer $n$ ($3 \le n \lt 10^5$), the number of characters in the string entered in the document. It is guaranteed that $n$ is divisible by $3$. 32 33 The second line of each test case contains a string of length $n$ consisting of only the characters T and M. 34 35 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. 36 37 Output 38 39 For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise. 40 41 Example 42 43 Input 44 45 ``` 46 5 47 3 48 TMT 49 3 50 MTT 51 6 52 TMTMTT 53 6 54 TMTTTT 55 6 56 TTMMTT 57 ``` 58 59 Output 60 61 ``` 62 YES 63 NO 64 YES 65 NO 66 YES 67 ``` 68 69 Note 70 71 In the first test case, the string itself is already a sequence equal to TMT. 72 73 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.