C. Nice Garland-------字符串
Posted -citywall123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C. Nice 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 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.
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.
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.
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 ", ans); for (int i = 0; i < n; i++) printf("%c", s2[i]); return 0; }
以上是关于C. Nice Garland-------字符串的主要内容,如果未能解决你的问题,请参考以下文章