cf——TMT Document

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 nn 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 aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero) characters.

输入格式
The first line contains an integer tt ( 1 \le t \le 50001≤t≤5000 ) — the number of test cases.

The first line of each test case contains an integer nn ( 3 \le n < 10^53≤n<10
5
), the number of characters in the string entered in the document. It is guaranteed that nn is divisible by 33 .

The second line of each test case contains a string of length nn consisting of only the characters T and M.

It is guaranteed that the sum of nn over all test cases does not exceed 10^510
5
.

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

题意翻译
给定一个由字符 T 和字符 M 构成的、长度为 nn 的字符串 ss,你要把它划分成若干个不相交的子序列,使得所有子序列都等于 TMT。判断是否有解,有解输出 YES,无解输出 NO。TT 组数据。
输入输出样例
输入
5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT
输出
YES
NO
YES
NO
YES
手动分割线
其实我最开始想麻烦了,我一直觉得要不断组成TM去配对,最后看T是不是够,这种思维可以用于基础的嵌套“TMTMTT",但是相对复杂的就不可以了,”TTTMTMTMT".其实核心是后面的T,就是让离M最近的T进行配对。因为这个题的TMT有两种方式,一种是嵌套进行的,TMTMTMTTT,另一种是并列进行的,TMTTMT,显然第一种是判断的重点,所以我们只需要让最近的T与M去配对。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
	int testcase;
	cin >> testcase;
	while (testcase--) {
		int n;
		cin >> n;
		string temp;
		cin >> temp;
		int flag = 1;
		int judge = 0;
		int t = 0;
		int m = 0;
		int len = temp.size();
		for (int i = 0; i < len; i++) {
			if (temp[i] == 'T') {
				t++;
				if (judge) {
					judge--;
				}
			}
			else {
				m++;
				judge++;
				if (m > t) {
					flag = 0;
					goto loop;
				}
			}
		}
	loop:;
		if (judge || t != 2 * m) {
			flag = 0;
		}
		if (flag) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
	return 0;
}

return code;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值