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 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;
}

  


上一篇:jQuery 鼠标拖拽排序


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