Codeforces-D-Diverse 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 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
思路:根据中间的改
代码;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char a[200005];
int main()
{
int n;
cin>>n;
scanf("%s",a);
int s=0;
for(int t=0;t<n-1;t++)
{
if(a[t]==a[t+1])
{
s++;
if(a[t]!='R'&&a[t+2]!='R')
{
a[t+1]='R';
}
if(a[t]!='G'&&a[t+2]!='G')
{
a[t+1]='G';
}
if(a[t]!='B'&&a[t+2]!='B')
{
a[t+1]='B';
}
}
}
cout<<s<<endl;
puts(a);
return 0;
}
Codeforces-D-Diverse Garland(思维)的更多相关文章
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- CodeForces 483C Diverse Permutation
Diverse Permutation Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Educational Codeforces Round 60 C 思维 + 二分
https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- codeforces 483C.Diverse Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/483/C 题目意思:给出 n 和 k,要求输出一个含有 n 个数的排列 p1, p2, ...,pn,使得 ...
随机推荐
- x264中重要结构体参数解释,参数设置,函数说明 <转>
x264中重要结构体参数解释http://www.usr.cc/thread-51995-1-3.htmlx264参数设置http://www.usr.cc/thread-51996-1-3.html ...
- @Value在Controller中取值
一.使用 @Value("${name}")注解可以获取自定义的properties文件中的name值 二.配置 如果只是在applicationcontext.xml中配置,那么 ...
- Velocity根据模版生成静态html
新公司的一个CMS项目要用到,这里记录下 一.项目文件图 二.springmvc-servlet.xml 添加 <!-- 定义环境变量文件 --> <bean id="pr ...
- strophe与openfire模拟的XMPP简单hello程序
/* * 参考<XMPP高级编程+使用JavaScript和jQuery>第三章例子. * 我修改了作者的XMPP服务器到本地的openfire.JavaScript跨域请求,使用Ngin ...
- 巧用cssText属性
给一个HTML元素设置css属性,如 1 2 3 4 var head= document.getElementById("head"); head.style.width = & ...
- Condition实现等待、通知
使用Condition实现等待/通知: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.L ...
- STM32 C++编程 002 GPIO类
使用 C++ 语言给 STM32 编写一个 Gpio 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的专 ...
- 算法Sedgewick第四版-第1章基础-023-MultiwordSearch.java
Multi-word search. Program MultiwordSearch.java reads a sequence of query words q[1], ..., q[k] from ...
- eclipse workspace 共享设置
总结一下,复制工作空间配置步骤如下: 1 使用eclipse新建workspace. 2 将新建的workspace下的.metadata\.plugins内容全部删除. 3 将需要拷贝的worksp ...
- 用MODI OCR 21种语言
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.12.08更新:2012.07.09按照<MODI中的OCR模块>一文相关内容进行修订2012.07.0 ...