D. Diverse Garland
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 diverse.

A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 11) have distinct colors.

In other words, if the obtained garland is tt then for each ii from 11 to n−1n−1 the condition ti≠ti+1ti≠ti+1 should be satisfied.

Among all ways to recolor the initial garland to make it diverse 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 diverse garland from the given one.

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

Examples
input

Copy
9
RBGRRBRGG
output

Copy
2
RBGRGBRGR
input

Copy
8
BBBGBRRR
output

Copy
2
BRBGBRGR
input

Copy
13
BBRRRRGGGGGRR
output

Copy
6
BGRBRBGBGBGRG 这个题目可以用暴力枚举+贪心直接来写,很快,也可以用dp写,不过dp我觉得比较复杂,所以我放弃了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
char s[maxn];
int main()
{
int n,ans=0;
cin >> n;
cin >> s;
if(n==1)
{
printf("0\n%s\n", s);
return 0;
}
int len = strlen(s);
for(int i=1;i<len;i++)
{
if(s[i]==s[i-1])
{
ans++;
s[i] = 'R';
if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
s[i] = 'B';
if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
s[i] = 'G';
if(s[i]!=s[i+1]&&s[i]!=s[i-1]) continue;
}
}
printf("%d\n", ans);
printf("%s\n", s);
return 0;
}

  


D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心的更多相关文章

  1. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  3. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...

  4. Codeforces Round #535 (Div. 3) 解题报告

    CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...

  5. Codeforces Round #535(div 3) 简要题解

    Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...

  6. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

  7. Codeforces Round #535 (Div. 3) 1108C - Nice Garland

    #include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.t ...

  8. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  9. Codeforces Round #535 (Div. 3)

    E: 题意: 给出n个整数ai和m个区间[li,ri] 你可以选择一些区间,并且将区间内的数字都减一.你要选择一些区间,然后使得改变后的数列中maxbi-minbi的值最大. 题解: 假设我们已经知道 ...

随机推荐

  1. H5 五子棋源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. Centos7.6 在LNMP上部署禅道

    一.下载禅道,并传到你的服务器上面的/opt文件下. http://dl.cnezsoft.com/zentao/7.3/ZenTaoPMS.7.3.stable.zbox_64.tar.gz 二.使 ...

  3. 利用OpenCV给图像添加中文标注

    利用OpenCV给图像添加中文标注 : 参考:http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html  和https://blog.csdn.net/ ...

  4. C-Linux_定时器示例使用

    #include <stdlib.h> #include <signal.h> ; static struct itimerval oldtv; struct itimerva ...

  5. JavaScript 笔记(二) 正则表达式

    正则表达式 Regular Expression (regex regexp re) str.search(正则表达式) 检索str字符串 返回子串起始位置 str.replace(正则表达式,'要替 ...

  6. MySQL主从 常见的错误及解决方案

    一.错误日志解析: (1) [ERROR]1452:无法在外键的表插入参考主键没有的数据 1452:无法在外键的表插入或更新参考主键没有的数据.由于item_discovery.itemid字段(外键 ...

  7. undefined 与 xx is not defined 的区别

    undefined 与 xx is not defined 的区别 1. undefined 表示是javascript中的一种数据类型,当被定义的变量没有被赋值或者某个被调用的函数没有定义返回值时候 ...

  8. @meda媒体查询

    定义和使用 使用 @media 查询,你可以针对不同的媒体类型定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@media 是非常有用的. ...

  9. 如何用ABP框架快速完成项目 - 自动化测试 - 前端angular e2e protractor

    要想快速完成一个项目, 自动化是很关键很有用的一块. 自动化测试比人工测试快很多. 特别是在回归测试中. 实践证明, 虽然投入了时间在写自动化测试代码上, 但是在回归测试中节省了大量的时间,同时及时发 ...

  10. Python 标准类库-数据类型之copy-深拷贝浅拷贝操作

    标准类库-数据类型之copy-深拷贝浅拷贝操作   by:授客 QQ:1033553122 Python中赋值并不会拷贝对象,只是创建目标和对象的绑定关系. copy.copy(x) 返回x的浅拷贝 ...