zoukankan      html  css  js  c++  java
  • 1.23 codeforces div3 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 |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
    3
    BRB
    
    output
    Copy
    1
    GRB
    
    input
    Copy
    7
    RGBGRBB
    
    output
    Copy
    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;
    } 
    
  • 相关阅读:
    更改文件默认打开方式
    python数据分析高频词提取,pyecharts词云制作并保存
    pyecharts V1.x版本使用Map绘制地图修改主题背景色等
    设置随机请求头和使用代理
    【重学前端】JS基础-原型和原型链
    【重学前端】JS基础-变量和类型
    Bootstrap blog整页制作
    拉勾网 移动端流式布局与rem布局整页制作
    PC端管理后台整页制作
    QQ飞车官方首页(部分)制作
  • 原文地址:https://www.cnblogs.com/stul/p/10314277.html
Copyright © 2011-2022 走看看