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).ios
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.ide
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".this
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.atom
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lamps.spa
The second line of the input contains the string ss consisting of nn characters 'R', 'G' and 'B' — colors of lamps in the garland.code
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.xml
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.blog
3 BRB
1 GRB
7 RGBGRBB
3 RGBRGBR
next_permutation(a, a + 3)
輸出數組a的字典序的全排列。
eg
#include<iostream> #include<algorithm> using namespace std; int main() { int ans[4]={1,2,3,4}; sort(ans,ans+4); /* 這個sort能夠不用,由於{1,2,3,4}已經排好序*/ do /*注意這步,若是是while循環,則須要提早輸出*/ { for(int i=0;i<4;++i) cout<<ans[i]<<" "; cout<<endl; }while(next_permutation(ans,ans+4)); return 0; }
#include<iostream> #include<algorithm> #include<string.h> #include<map> #include<stdio.h> #include<math.h> using namespace std; #define ll long long using namespace std; const int maxn = 2e5 + 10; char str[maxn], s1[maxn], s2[maxn]; int n, a[4]; int main() { cin >> n >> str; map<int, char> m; m[0] = 'R'; m[1] = 'G'; m[2] = 'B'; int ans = maxn * 2; a[1] = 1, a[2] = 2; do { s1[0] = m[a[0]]; s1[1] = m[a[1]]; s1[2] = m[a[2]]; for (int q = 3; q < n; q++) { s1[q] = s1[q - 3]; } int cnt = 0; for (int q = 0; q < n; q++) { if (s1[q] != str[q]) cnt++; } if (cnt < ans) { ans = cnt; strcpy(s2, s1); } } while (next_permutation(a, a + 3)); printf("%d\n", ans); for (int i = 0; i < n; i++) printf("%c", s2[i]); return 0; }