C. Nice 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 nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is tt, then for each i,ji,j such that ti=tjti=tj should be satisfied |i−j| mod 3=0|i−j| mod 3=0. The value |x||x| means absolute value of xx, the operation x mod yx mod y means remainder of xx when divided by yy.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice 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 nice garland from the given one.
In the second line of the output print one string tt of length nn — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
Examples
input
Copy
3 BRB
output
Copy
1 GRB
input
Copy
7 RGBGRBB
output
Copy
3 RGBRGBR
感觉能写出来的都是天才,题目是说相同的字母相隔的距离要为三的倍数,所以最后的答案一定是类似于RGBRGB之类的循环,在RGB六种全排列中一一比对,看看哪种情况操作最小就行。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int manx = 2e5 + 10;
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
string a[6] = { "RGB", "RBG", "GRB", "GBR", "BRG","BGR" };//一共6种情况
int ans = 0x3f3f3f3f, xx = -1;
for (int i = 0; i < 6; i++)
{
int cnt = 0;
for (int j = 0; j < s.size(); j++)
{
if (a[i][j % 3] != s[j])cnt++;
}
if (cnt < ans)
{
ans = cnt;
xx = i;//记录最小操作数是哪种排列
}
}
cout << ans <<endl;
for (int i = 0; i < s.size(); i++)
{
cout << a[xx][i % 3];
}
cout << endl;
}
int main()
{
solve();
}