D. Diverse Garland
Posted studyshare777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Diverse Garland相关的知识,希望对你有一定的参考价值。
You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the ii-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
In other words, if the obtained garland is tt then for each ii from 11 to n−1n−1 the condition ti≠ti+1should 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⋅1051≤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.
Examples
input
9
RBGRRBRGG
output
2
RBGRGBRGR
input
8
BBBGBRRR
output
2
BRBGBRGR
input
13
BBRRRRGGGGGRR
output
6
BGRBRBGBGBGRG
题目描述:
两个相邻之间颜色不能相同,问最少修改颜色多少次。
分析:
每2个比较一次,相同换一下就行。最后的位置再特判一下。
代码:
#include <iostream> #include <algorithm> #include <cstring> using namespace std; char a[200009]; void vh(int i,char ch) { if(a[i+1]!=‘G‘&&ch!=‘G‘) a[i]=‘G‘; else if(a[i+1]!=‘R‘&&ch!=‘R‘) a[i]=‘R‘; else a[i]=‘B‘; } int main() { int n; cin>>n; getchar(); for(int i=0;i<n;i++) { a[i]=getchar(); } int ans=0; int m; for(int i=1;i<n-1;i++) { if(a[i-1]==a[i]) { vh(i,a[i]); ans++; } } if(a[n-1]==a[n-2]) { ans++; if(a[n-2]!=‘G‘) a[n-1]=‘G‘; else if(a[n-2]!=‘R‘) a[n-1]=‘R‘; else a[n-1]=‘B‘; } printf("%d ",ans); for(int i=0;i<n;i++) printf("%c",a[i]); return 0; }
以上是关于D. Diverse Garland的主要内容,如果未能解决你的问题,请参考以下文章