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. 15.3. REST
  2. Python错误“ ImportError:未命名模块”
  3. 四种依恋类型_依恋类型的四种模式影响着我们的亲密关系
  4. Linux Kernel中断下半部分实现的三种方式
  5. 2.odoo13的学习(odoo中的模型、模型类属性、字段属性等)
  6. java如何解决高并发问题_java怎么处理高并发?
  7. DOxygen for C++使用说明——注释代码二
  8. 【教你一招】30分钟考完广开所有科目,广开期末考试网页如何多开?
  9. win10主机前置耳机孔无声音,但是后置有声音,按照下面的设置进行设计即可
  10. 打开量化交易的黑箱——note1
  11. SIM800C通讯模块调试
  12. 数字传感器:新兴的应用
  13. 移动式无线蓝牙RFID+二维码扫描一体机|读卡器HX-R58C-B在安卓与苹果手机上如何操作?
  14. am335x开发板的疑问以及解答
  15. Web3.0与数字时尚,该如何落地?
  16. 马云谈教师惩戒权:没有惩戒,就如让老师开一辆没刹车的汽车
  17. python实现小米手机 dingding 自动打卡
  18. 初级***常备工具包大全
  19. 安利几个翻译照片的好用软件
  20. 机器学习中样本的样本量的估计

热门文章

  1. 安装软件出现提示:无法将数值写入键
  2. 清除COOKIES有什么好处
  3. 乱序整数序列两数之和绝对值最小
  4. 英语中最常用的252个词根
  5. HR最不认同的5大跳槽理由排行榜
  6. 白马非马的面向对象分析
  7. 彪悍开源的分析数据库-ClickHouse
  8. php强类型作用,强类型php性能很彪悍
  9. 远程学习(一):基于ZeroTier的异地组网及Moon转发节点搭建
  10. jQuery根据纬度经度查看地图