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("%d\n",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相关推荐

  1. K-periodic Garland CodeForces - 1353E(贪心)

    You are given a garland consisting of n lamps. States of the lamps are represented by the string s o ...

  2. K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)

    题意: 给定长为 n 的 0, 1 字符串,你可以通过一次操作改变一个字符(0 变 1 or 1 变 0),问最少几次操作可以使任意相邻两个 1 之间的距离为 k ? 题目: You are give ...

  3. 1108D. Diverse Garland

    D. Diverse Garland:题目 什么脑瘫题目!!!可恶,和dp有什么关系?但是强迫症让我不得不写,空一个很难受!! #include <bits/stdc++.h> using ...

  4. Codeforces 1287C Garland

    题目链接: Codeforces 1287C Garland 思路: 我们记dp[i][j][0]和dp[i][j][1]分别为第i个为奇数/偶数且前i个里面有j个偶数的情况下,第i个的最小复杂度: ...

  5. 题解 UVA1555 【Garland】(二分)

    题面 最近有人问我这个题这么做,光讲几句貌似没什么用,干脆发个题解吧(话说什么有人会来问我这个蒟蒻) 题面的意思大概就是说每个点的高度都和旁边2个点有关,整张图没有高度小于0的点,求最后一个点B的最低 ...

  6. codeforces 767C - Garland

    题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...

  7. CF767C Garland

    一道树形DP \(Translate\) 有一颗\(n\)个节点的树 第\(i\)个节点权值为\(a_i\) \((n<=10^6,-100<=a_i<=100)\) 问是否能够删除 ...

  8. CodeForces - 1353E K-periodic Garland(思维+dp)

    题目链接:点击查看 题目大意:给出 n 个灯泡以及其初始状态(开或关),每次操作可以将任意一个灯泡的状态置反,问最少需要操作多少次,可以使得所有开着的灯泡之间相距 k 个单位 题目分析:因为需要满足所 ...

  9. CodeForces - 1287C Garland(贪心)

    题目链接:点击查看 题目大意:原本有一个1~n的排列,现在缺少了一些数字,用0来代替,现在要求将缺少的数字重新填回去,要求代价和最少,规定代价为:若相邻两个数字的奇偶性不同,则代价为1,否则代价为0 ...

  10. E:K-periodic Garland(DP)

    思路 每个点我们有两种决策,其值为0或1: 如果点我们放置0的话,我们有其前一位数字是零,或者其前一位数字是一. 如果这个点我们放置1的话,我们有其前面是按照每k个数字都出现一次1的排列,也有可能其前 ...

最新文章

  1. Eclipse中Junit测试中@Before不执行
  2. Redis 数据同步机制分析
  3. 阿里云知位停车:让车主好停、车场好管
  4. 最小费用最大流背诵用模板
  5. linux 下 libpcap 简单使用
  6. 【转】Python自动化测试 (一) Eclipse+Pydev 搭建开发环境
  7. 今日看了一下广告收入,心里拔凉拔凉的。
  8. BZOJ_1096_[ZJOI2007]_仓库建设_(斜率优化动态规划+单调队列+特殊的前缀和技巧)
  9. 用FileZillaServer搭建FTP服务服务端、客户端
  10. canvas压缩图片方法
  11. 蓝牙服务器维修理地址,云服务器蓝牙
  12. js 封装cookie
  13. 科技爱好者周刊(第 106 期):数字游民
  14. 订单(一)——准备订单数据
  15. 《大学计算机基础》课程学习(1)——第1章 计算机基础知识
  16. 不做无用功 Google Colab掉线自动重连“助手”
  17. 基于XP系统IE极光漏洞渗透
  18. Unity VR中实现播放3D电影
  19. 删除电脑中的顽固软件
  20. 解决The page at ‘xxx‘ was loaded over HTTPS

热门文章

  1. MoSE论文中Sequential Synthetic Dataset生成代码(时间序列多任务学习数据集)
  2. 【立项建议书】苏科大校园app立项建议书
  3. JS 根据date日期格式返回周几
  4. MAC 下ifconfig看IP
  5. rabbitmq:publisher confirms
  6. 端口映射vc++6.0编译【转】
  7. 自定义皮肤,你值得拥有!
  8. chrome driver 环境问题
  9. 语音验证码与语音验证码APISDK接口
  10. CentOS 7系统语言修改成英文