CF #738(div2)B. Mocha and Red and Blue(构造)

举报
小哈里 发表于 2022/05/10 23:30:53 2022/05/10
【摘要】 problem B. Mocha and Red and Blue time limit per test1 second memory limit per test256 megabytes inpu...

problem

B. Mocha and Red and Blue
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 n 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 3, 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 t (1≤t≤100) — the number of test cases. Each test case consists of two lines.

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

The second line of each test case contains a string s with length n, 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.

Example
inputCopy
5
7
?R???BR
7
???R???
1
?
1
B
10
?R??RB??B?
outputCopy
BRRBRBR
BRBRBRB
B
B
BRRBRBBRBR
Note
In the first test case, if the squares are painted “BRRBRBR”, the imperfectness is 1 (since squares 2 and 3 have the same color), which is the minimum possible imperfectness.

B. 摩卡和红蓝
每次测试的时间限制1秒
每个测试的内存限制 256 兆字节
输入标准输入
输出标准输出
随着他们的故事解开,一个永恒的故事再次被讲述…

摩卡的朋友白姬热衷于玩音乐游戏Arcaea并分享摩卡有趣的谜题来解决。这一天,白姬想出了一个新的简单谜题,并希望摩卡能够解决它们。然而,这些谜题对于摩卡来说太容易解决了,所以她希望你解决它们并告诉她答案。谜题描述如下。

有n个正方形排成一排,每个正方形都可以涂成红色或蓝色。

在这些方格中,有的已经画好了,有的则是空白的。您可以决定在每个空白方块上绘制哪种颜色。

一些相邻的方块可能具有相同的颜色,这是不完美的。我们将缺陷定义为共享相同颜色的相邻方块对的数量。

例如,“BRRRBBR”的不完美度为3,“BB”出现一次,“RR”出现两次。

你的目标是尽量减少不完美,并在绘画后打印出正方形的颜色。

输入
每个测试包含多个测试用例。

第一行包含一个整数 t (1≤t≤100)——测试用例的数量。每个测试用例由两行组成。

每个测试用例的第一行包含一个整数 n (1≤n≤100)——正方形行的长度。

每个测试用例的第二行包含一个长度为 n 的字符串 s,包含字符 ‘B’、‘R’ 和 ‘?’。这里’B’代表蓝色方块,‘R’代表红色方块,’?'为一个空白方块。

输出
对于每个测试用例,打印一行只包含 ‘B’ 和 ‘R’ 的字符串,即绘制后正方形的颜色,将缺陷最小化。如果有多个解决方案,请打印其中任何一个。

例子
输入副本
5
7
?R???BR
7
???R???
1
?
1

10
?R??RB??B?
输出副本
BRRBRBR
BRRBRBRB


BRRBRBBRBR
笔记
在第一个测试案例中,如果方块被涂成“BRRBRBR”,则缺陷为 1(因为方块 2 和 3 具有相同的颜色),这是最小可能的缺陷。

solution

对于最长的“ ? ”,它优化了“ RBRB… ”或“ BRBR… ”的涂色,所以它所做的不完美仅与它两侧的颜色有关。

为每个最长的“ ? ”周期选择不完美的一个○ ( n ) 是可以接受的。

更优雅的是,如果“ ? ”左边或右边的方块是画的,只需用与它不同的颜色画“ ? ”。这可以通过考虑奇偶性来证明达到最小缺陷。

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;  cin>>T;
	while(T--){
		int n;  cin>>n;
		string s;  cin>>s;
		for(int i = 1; i < n; i++){
			if(s[i]=='?'){
				if(s[i-1]=='B')s[i]='R';
				if(s[i-1]=='R')s[i]='B';
			}
		}
		for(int i = n-2; i>=0; i--){
			if(s[i]=='?'){
				if(s[i+1]=='B')s[i]='R';
				if(s[i+1]=='R')s[i]='B';
			}
		}
		if(s[0]=='?'){
			for(int i = 0; i < n; i++){
				if(i%2==0)cout<<"B";
				else cout<<"R";
			}
			cout<<"\n";
			continue;
		}
		cout<<s<<"\n";
	}
	return 0;
}


  

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/119741727

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。