我是靠谱客的博主 犹豫自行车,最近开发中收集的这篇文章主要介绍Nice Garland,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi (‘R’, ‘G’ and ‘B’ — colors of lamps in the garland).

You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.

A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is tt, then for each i,ji,j such that ti=tjti=tj should be satisfied |i−j| mod 3=0|i−j| mod 3=0. The value |x||x| means absolute value of xx, the operation x mod yx mod y means remainder of xx when divided by yy.

For example, the following garlands are nice: “RGBRGBRG”, “GB”, “R”, “GRBGRBG”, “BRGBRGB”. The following garlands are not nice: “RR”, “RGBG”.

Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.

Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lamps.

The second line of the input contains the string ss consisting of nn characters ‘R’, ‘G’ and ‘B’ — colors of lamps in the garland.

Output
In the first line of the output print one integer rr — the minimum number of recolors needed to obtain a nice garland from the given one.

In the second line of the output print one string tt of length nn — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.

Examples
Input
3
BRB
Output
1
GRB
Input
7
RGBGRBB
Output
3
RGBRGBR

题意是给出由R,G,B三个字符组成的字符串,长度为n,将这个字符串改为每个字符间间隔为2,并要求更改次数最少。输出最小更改次数并输出更改后的字符串。

将所有情况暴力选出更改次数最少的即可。本想每种情况都列举一次,但看了同学的做法发现可以用两层循环解决。
输出一段的代码可以简化,用一个for就可以(这里懒得改了。。。)

代码如下:

#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#include"algorithm"
using namespace std;
char a[200005];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		char aa[9][5]={"RGB","RBG","GBR","GRB","BGR","BRG"};
		int s[10],b=200005,c=0;
		memset(s,0,sizeof(s));
		memset(a,0,sizeof(a));
		scanf("%s",a);
		for(int i=0;i<6;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(a[j]!=aa[i][j%3])
				{
					s[i]++;
				}
			}
			if(s[i]<b)
			{
				b=s[i];
				c=i;
			}
		}
		printf("%dn",b);
		for(int i=0;i<n/3;i++)
		{
			printf("%s",aa[c]);
		}
		for(int i=0;i<n%3;i++)
		{
			printf("%c",aa[c][i]);
		}
		printf("n");
	}
	return 0;
} 

最后

以上就是犹豫自行车为你收集整理的Nice Garland的全部内容,希望文章能够帮你解决Nice Garland所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部