D - Colorful Stamp CodeForces - 1669D

https://codeforces.com/problemset/problem/1669/Dhttps://codeforces.com/problemset/problem/1669/D

A row of nnn cells is given, all initially white. Using a stamp, you can stamp any two neighboring cells such that one becomes red and the other becomes blue. A stamp can be rotated, i.e. it can be used in both ways: as BR\color{blue}{\texttt{B}}\color{red}{\texttt{R}}BR and as RB\color{red}{\texttt{R}}\color{blue}{\texttt{B}}RB.

During use, the stamp must completely fit on the given nnn cells (it cannot be partially outside the cells). The stamp can be applied multiple times to the same cell. Each usage of the stamp recolors both cells that are under the stamp.

For example, one possible sequence of stamps to make the picture BRBBW\color{blue}{\texttt{B}}\color{red}{\texttt{R}}\color{blue}{\texttt{B}}\color{blue}{\texttt{B}}\texttt{W}BRBBW could be WWWWW→WWRB‾W→BR‾RBW→BRB‾BW\texttt{WWWWW} \to \texttt{WW}\color{brown}{\underline{\color{red}{\texttt{R}}\color{blue}{\texttt{B}}}}\texttt{W} \to \color{brown}{\underline{\color{blue}{\texttt{B}}\color{red}{\texttt{R}}}}\color{red}{\texttt{R}}\color{blue}{\texttt{B}}\texttt{W} \to \color{blue}{\texttt{B}}\color{brown}{\underline{\color{red}{\texttt{R}}\color{blue}{\texttt{B}}}}\color{blue}{\texttt{B}}\texttt{W}WWWWW→WWRB​W→BR​RBW→BRB​BW. Here W\texttt{W}W, R\color{red}{\texttt{R}}R, and B\color{blue}{\texttt{B}}B represent a white, red, or blue cell, respectively, and the cells that the stamp is used on are marked with an underline.

Given a final picture, is it possible to make it using the stamp zero or more times?

Input

The first line contains an integer ttt (1≤t≤1041 \leq t \leq 10^41≤t≤104) — the number of test cases.

The first line of each test case contains an integer nnn (1≤n≤1051 \leq n \leq 10^51≤n≤105) — the length of the picture.

The second line of each test case contains a string sss — the picture you need to make. It is guaranteed that the length of sss is nnn and that sss only consists of the characters W\texttt{W}W, R\texttt{R}R, and B\texttt{B}B, representing a white, red, or blue cell, respectively.

It is guaranteed that the sum of nnn over all test cases does not exceed 10510^5105.

Output

Output ttt lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if it possible to make the picture using the stamp zero or more times, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Sample 1

InputcopyOutputcopy
12
5
BRBBW
1
B
2
WB
2
RW
3
BRB
3
RBB
7
WWWWWWW
9
RBWBWRRBW
10
BRBRBRBRRB
12
BBBRWWRRRWBR
10
BRBRBRBRBW
5
RBWBW

YES
NO
NO
NO
YES
YES
YES
NO
YES
NO
YES
NO

思路:我们可以先判断n==1||n==2的情况,然后再判断n>=3的情况.

n==1:只有一个字符的时候我们不能染色,这时候只要有R或者B的情况都直接输出NO。

n==2: 这时候如果染色的话就不能出现W,如果不染色的话就不能出现R和B。

n>=3: 这种情况我们可以判断两个W之间的字符,两个W之间的字符不能全是一样的,只要两个W字符之间既有R又有B则正确,否则的话输出NO。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		string s;
		cin>>s;
		bool flag=true;
		if(n==1)
		{
			if(s=="R"||s=="B")
			{
				cout<<"NO"<<endl;flag=false;
				continue;
			}
		}
		if(n==2)
		{
			if(s=="BB"||s=="RR"||s=="WB"||s=="BW"||s=="WR"||s=="RW")
			{
				cout<<"NO"<<endl;flag=false;
				continue;
			}
		}
		s='W'+s;
		s+='W';
		string nn="";
		for(int i=0; i<s.size(); i++)
		{
			if(s[i]=='W')
			{
				if(nn.size()!=0)
				{
					bool f1=true,f2=true;
					for(int j=0;j<nn.size();j++)
					{
						if(nn[j]=='R') f1=false;
						if(nn[j]=='B') f2=false;
					}
					if(f1||f2)
					{
						cout<<"NO"<<endl;flag=false;
						break;
					}
				}
				nn="";
			}
			else
				nn+=s[i];
		}
		if(flag) cout<<"YES"<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值