我是靠谱客的博主 甜美红牛,这篇文章主要介绍C. Nice Garland-------字符串,现在分享给大家,希望可以做个参考。

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 |ij| 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 (1n21051≤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
复制代码
1
2
3 BRB
output
Copy
复制代码
1
2
1 GRB
input
Copy
复制代码
1
2
7 RGBGRBB
output
Copy
复制代码
1
2
3 RGBRGBR


next_permutation(a, a + 3)
输出数组a的字典序的全排列。

eg
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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; }

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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("%dn", ans); for (int i = 0; i < n; i++) printf("%c", s2[i]); return 0; }

 

转载于:https://www.cnblogs.com/-citywall123/p/10316570.html

最后

以上就是甜美红牛最近收集整理的关于C. Nice Garland-------字符串的全部内容,更多相关C.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部