跳到正文

剪贴板

剪贴板 noxfxfu2

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<iostream>
#include<deque>
using namespace std;
#define NO return cout<<"No\n",void()
string str;
deque<int> x,y;
void mian()
{
	x.clear(),y.clear();
	int n;
	int cx=0,cy=0,cz=0;
	cin>>n>>str;
	for(char ch:str)
	{
		if(ch=='X')	cx++;
		if(ch=='Y')	cy++;
		if(ch=='Z')	cz++;
	}
	if(cx>n || cy>n || cz>n)	NO;
	int yz=(cy+cz-cx)/2;
	for(int i=0;i<2*n;i++)
		if(str[i]=='X')			x.push_back(i);
		else if(str[i]=='Y')	y.push_back(i);
		else
		{
			if(y.size() && yz)	y.pop_front(),yz--;
			else if(x.size())	x.pop_back();
			else	NO;
		}
	while(x.size())
	{
		if(!y.size() || x.front()>y.front())	NO;
		else	x.pop_front(),y.pop_front();
	}
	cout<<"Yes\n";
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)	mian();
}