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

此题题义理解之后可以知道GRB三种灯怎样排列只会有种6种 nice garland 的方式,但是要算改变最小的,所以可以采用比较法,比较用哪种排列方法会使所改变次数最小而获得 nice garland 然后输出。代码如下:

在这里插入代码片

#include<stdio.h>
#include<string.h>
#define INF 1 << 30
int num[6][3]={{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};
int n,a[200005],t,s=INF,x;
char ch[200005]; 
int main()
{
     while(~scanf("%d", &n))
     {
		scanf("%s",ch);	
		for(int i = 1; i <= n; i++) 
	 	{
			a[i] = ch[i] == 'R' ? 1 : ch[i] == 'G' ? 2 : 3;
	 	}
		for(int i = 0; i <= 5; i++) 
	  	{
      		t=0;
         	for(int j = 1; j <= n; j++) t+=a[j] != num[i][j%3];
      	 	{
	 	      	if(t<s)
	   			{
			   		s = t; 
			   		x = i;
			    }
			}    
  		}
  		printf("%d\n",s);
     	for(int i = 1; i <= n; i++) 
	 	{
		 	printf(num[x][i%3] == 1 ? "R" : num[x][i%3] == 2 ? "G" : "B");
	 	}
	 	printf("\n");
     } 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值