ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces-1559B (思维题)

2021-10-05 20:33:06  阅读:255  来源: 互联网

标签:case 思维 them 1559B imperfectness Codeforces each test squares


1.题目引入:

As their story unravels, a timeless tale is told once again...

Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.

There are nn squares arranged in a row, and each of them can be painted either red or blue.

Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.

Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.

For example, the imperfectness of "BRRRBBR" is 33, with "BB" occurred once and "RR" occurred twice.

Your goal is to minimize the imperfectness and print out the colors of the squares after painting.

Input

Each test contains multiple test cases.

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Each test case consists of two lines.

The first line of each test case contains an integer nn (1≤n≤1001≤n≤100) — the length of the squares row.

The second line of each test case contains a string ss with length nn, containing characters 'B', 'R' and '?'. Here 'B' stands for a blue square, 'R' for a red square, and '?' for a blank square.

Output

For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.

2.样例输出: 

Input

5
7
?R???BR
7
???R???
1
?
1
B
10
?R??RB??B?

Output

BRRBRBR
BRBRBRB
B
B
BRRBRBBRBR

Note

In the first test case, if the squares are painted "BRRBRBR", the imperfectness is 11 (since squares 22and 33 have the same color), which is the minimum possible imperfectness.

题目大意:给定一个字符串需要将 ?改为 ‘R’ 或 ‘B’,保证连续的字符串降到最小, 可以将字符串从前到后推,再将他们从后往前推,但这两种做法都需要第一个字符不是 ‘ ?’,具体操作如下:

3.代码如下: 

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main()
{
	ios::sync_with_stdio(false); 
	int n;
	cin>>n;
	while(n--)
	{
		int t;
		string s="";
		cin>>t;
		cin>>s;
		for(int i=1;i<t;i++)   // 由前推后 
		{
			if(s[i]=='R'||s[i]=='B')
			{
			//	i++;
				continue;
			}
			else
			{
				if(s[i-1]=='B') s[i]='R';
				if(s[i-1]=='R') s[i]='B'; 
			}
		}
		for(int i=t-2;i>=0;i--)  // 由后推前 
		{
			if(s[i]=='R'||s[i]=='B')
			{
				continue;
			}
			else
			{
				if(s[i+1]=='B') s[i]='R';
				if(s[i+1]=='R') s[i]='B'; 
		    }
		}
		if(s[0]=='?')   // 显然前两种都必须是第一个字符和第二个字符都不是 "?" 
		{
			for(int i=0;i<t;i++)
			{
				if(i&1) cout<<"R";
				else    
				{
					cout<<"B";
				}
			}
			cout<<"\n";
			continue;
		}
		else
		 cout<<s<<"\n";
	}
	return 0;
}

标签:case,思维,them,1559B,imperfectness,Codeforces,each,test,squares
来源: https://blog.csdn.net/weixin_52914088/article/details/120617606

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有