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;
    } 
    
  • 相关阅读:
    我在硅谷时候写的文章
    快速上手ABP
    朝花夕拾
    ABP问题速查表
    被低估的.net(上)
    申请Office 365一年免费的开发者账号攻略(2018年10月份版本)
    office365的开发者训练营,免费,在微软广州举办
    数据恢复顾问(DRA)
    使用RMAN对数据文件进行恢复
    Linux环境下利用句柄恢复Oracle误删除的数据文件
  • 原文地址:https://www.cnblogs.com/stul/p/10314277.html
Copyright © 2011-2022 走看看