GDUT 2020寒假训练 排位赛四 B

原题链接

  • B. Diverse Garland

题目


You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the i-th lamp is si (‘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 1) have distinct colors.

In other words, if the obtained garland is t then for each i from 1 to n−1 the condition ti≠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 n (1≤n≤2⋅105) — the number of lamps.

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

Output
In the first line of the output print one integer r — 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 t of length n — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.

样例

input
9
RBGRRBRGG
output
2
RBGRGBRGR

input
8
BBBGBRRR
output
2
BRBGBRGR

input
13
BBRRRRGGGGGRR
output
6
BGRBRBGBGBGRG

题目大意

给出一串仅含有‘R’‘G’‘B’的字符串,要想让字符串中相邻两个字符都不相同,求出最少要改变的字符个数,输出其中一种改变后的结果

思路

模拟
遍历字符串,检查当前位的字符是否与前一个字符相同,若相同,就在RGB这三个字符中挑一个字符,满足这个字符不与前一个还有后一个字符相同。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
string str;
char change[6]={'R','G','B','R','G','B'};
int main()
{int n;cin>>n>>str;int ans=0;for(int i=1;i<n;i++){if(str[i]==str[i-1]){for(int j=0;j<3;j++){if(change[j]!=str[i-1]&&change[j]!=str[i+1]){str[i]=change[j];break;}}ans++;}}cout<<ans<<endl<<str<<endl;return 0;
}

B. Diverse Garland相关推荐

  1. 1108D. Diverse Garland

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

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

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

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

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

  4. DivCo: Diverse Conditional Image Synthesis via Contrastive Generative Adversarial Network

    模式崩溃是cGAN较为重要的问题,以往工作大多数都致力于关注latent code和生成图像之间的关系而没有关注通过不同latent code生成的图像之间的关系.MSGAN(mode seeking ...

  5. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 1 /* 2 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 3 这样保证不会超出边界并且以防其余的数相邻绝对值差>k 4 ...

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

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

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

  8. various,variable,varied,different, diverse的区别

    various 和 varied various 和 varied的意思都是"各种各样的,形形色色的",但强调点不同:various 侧重多样性,varied 侧重与众不同. 比较 ...

  9. C. Diverse Permutation(Codeforces Round #275(div2)

    C. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...

  10. StarGAN v2: Diverse Image Synthesis for Multiple Domains (多域多样性图像合成)

    前言 StarGAN v2: Diverse Image Synthesis for Multiple Domains  (多域多样性图像合成) In CVPR 2020. Paper: https: ...

最新文章

  1. java list移除所有元素,从List中移除指定 List 中包含的其所有元素(可选操作)。...
  2. vmware虚拟机不识别usb设备
  3. linux c账户安全性,linux c flash安全策略
  4. 网络:TCP维护安全可靠机制提供的定时器
  5. python与机器学习(七)上——PyTorch搭建LeNet模型进行MNIST分类
  6. easyui前端实现多选框_在实际案例中学习前端开发(第二期)
  7. java循环取出word table表格中所有的值
  8. MySQL存储过程 — 解析 XML 数据并实现插入操作
  9. PyCharm代码区不能编辑的解决办法
  10. apache的mpm工作模式
  11. 微信小程序DEMO初体验
  12. 免费「模拟面试」福利反馈连载(20180128期)
  13. 白盒测试工具CodeTest
  14. 《统计学》第八版贾俊平第五章概率与概率分布
  15. win8.1搭建php环境,WIN8.1下搭建PHP5.6环境
  16. 傻妞sillyGirl教程
  17. 3种方式构造HTTP请求详解(HTTP4)
  18. 分类算法-决策树、随机森林
  19. 新版个人所得税计算python_2019年新税率工资计算器(python脚本) | linux系统运维...
  20. Python 检测PE所启用保护方式

热门文章

  1. [游戏]求生之路超级专家难度模式
  2. 最近心血来潮,准备更新博客
  3. 浏览器主页被hao123劫持之解决方案
  4. python可以在多平台运行 体现了python语言的,Python 可以在多平台运行,这体现了 Python 语言的 特性。...
  5. 全自动与半自动手表的区别_全自动和半自动机械表的区别?
  6. 关于清华大学C语言门事件
  7. AS 使用Statistic插件统计代码行数
  8. Linux misc设备(二)蜂鸣器驱动
  9. javascript history对象详解
  10. python:网络爬虫之遍历单个域名获取电影名称及年份