Codeforces Round #715 (Div. 2)B. TMT Document

28 篇文章 0 订阅

传送门
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.

Input

The first line contains an integer t (1≤t≤5000) — the number of test cases.

The first line of each test case contains an integer n (3≤n<105), 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 105.

Output

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

思路:

直接遍历整个串,当找到一个‘T’后,就将t的计数器++,当找到一个‘M’后,表示它可以和一个‘T’配对,此时,T的计数器–,我们可以发现,当‘T’的计数器T < 0 || T > n/3时,是不可能满足题目要求的,因而:

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
#define ll long long     
char a[100010];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		scanf("%s",a+1);
		int cnt = n/3;
		int m = 0;
		int T = 0;
		int flag = 0;
		for(int i = 1; i <= n; i++)
		{
			if(a[i] == 'T')
			{
				T++;
			}
			else
			{
				m++;
				T--;
			}
			if(T < 0 || T > n/3)
			{
				flag = 1;
				break;
			}
		}
		if(flag || m != cnt)
		{
			printf("NO\n");
			continue;
		}
		else
		{
			printf("YES\n");
			continue;
		}
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值