Technical Support

传送门

You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.

Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.

Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.

You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1 \le t \le 5001≤t≤500). Description of the test cases follows.

The first line of each test case contains one integer nn (1 \le n \le 1001≤n≤100) — the total number of messages in the dialog.

The second line of each test case consists of nn characters "Q" and "A", describing types of messages in the dialog in chronological order. Character "Q" denotes the message with client question, and character "A" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to "Q".

Output

For each test case print "Yes" (without quotes) if dialog may correspond to the rules of work, or "No" (without quotes) otherwise.

Sample 1

InputcopyOutputcopy
5
4
QQAA
4
QQAQ
3
QAA
1
Q
14
QAQQAQAAQQQAAA
Yes
No
Yes
No
Yes

Note

In the first test case the two questions from the client are followed with two specialist's answers. So this dialog may correspond to the rules of work.

In the second test case one of the first two questions was not answered.

In the third test case the technical support manager sent two messaged as the answer to the only message of the client.

题解:只要每一个提问有且至少一个回复即可,因为回复可能延迟,所以选择从后面开始遍历。如果符合规则,必有 回复的次数大于等于询问的次数

#include<iostream>
using namespace std;
const int N = 505;
char c[N];


int main()

{
	int t;
	cin >> t;
	while (t--)
	{
		int n,ta=0,tq=0;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> c[i];
		}
		int f = 1;
		for (int i = n; i >=1; i--)
		{
			if (c[i] == 'Q')
			{
				tq++;
				if (tq > ta)
				{
					f = 0;
					cout << "NO" << endl;
					break;
				}
			}
			else
			{
				ta++;
			}
		}
		if (f)cout << "Yes" << endl;


	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值