概述
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
题意:输入一个字符串,按照GRB或者这三个字符组成的其他的顺序依次出现。计算哪些字符违反规则,并纠正。要使纠正次数最少,输出纠正次数和纠正过的字符串。
思路:使用next_permutation()函数,把GRB按照字典序组合一遍,然后再判断输入字符串与这6个组合的不同次数。找到最小的次数,保存这个组合。然后输出
下面是ac代码
#include<bits/stdc++.h>
using namespace std;
char p[3] = {'R','G','B'};
int ans = 1e9;
int main(){
string s,res;
int n;
cin>>n;
cin>>s;
sort(p,p+3);
do{
int count = 0;
for(int i = 0; i < n; i++)
if(s[i] != p[i%3]) // p 会溢出,所以 mod 3 。
count++;
if(count < ans)
ans = count, res = p;
}while(next_permutation(p,p+3));
cout<<ans<<endl;
for(int i = 0; i < n; i++)
{
cout<<res[i%3];
}
return 0;
}
转载于:https://www.cnblogs.com/stul/p/10314277.html
最后
以上就是健壮魔镜为你收集整理的1.23 codeforces div3 C.Nice Garland的全部内容,希望文章能够帮你解决1.23 codeforces div3 C.Nice Garland所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复