zoukankan      html  css  js  c++  java
  • Codeforces-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.

    Input

    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.

    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

    思路:一共有6种情况,选出一个改变最小的

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    char a[200005];
    char b[200005];
    char c[200005];
    int main() {
    	int n;
    	cin>>n;
    	scanf("%s",b);
    	int len=strlen(b);
    	strcpy(a,b);
    
    	int maxn=999999;
    
    	int s=0;
    
    
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='R'&&t<len) {
    			a[t]='R';
    			s++;
    		}
    		if(a[t+1]!='G'&&t+1<len) {
    			a[t+1]='G';
    			s++;
    		}
    		if(a[t+2]!='B'&&t+2<len) {
    			a[t+2]='B';
    			s++;
    		}
    	}
    
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    	strcpy(a,b);
    	s=0;
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='R'&&t<len) {
    			a[t]='R';
    			s++;
    		}
    		if(a[t+1]!='B'&&t+1<len) {
    			a[t+1]='B';
    			s++;
    		}
    		if(a[t+2]!='G'&&t+2<len) {
    			a[t+2]='G';
    			s++;
    
    		}
    
    	}
    
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    	strcpy(a,b);
    	s=0;
    
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='G'&&t<len) {
    			a[t]='G';
    			s++;
    		}
    		if(a[t+1]!='R'&&t+1<len) {
    			a[t+1]='R';
    			s++;
    		}
    		if(a[t+2]!='B'&&t+2<len) {
    			a[t+2]='B';
    			s++;
    		}
    	}
    
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    	strcpy(a,b);
    	s=0;
    
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='G'&&t<len) {
    			a[t]='G';
    			s++;
    		}
    		if(a[t+1]!='B'&&t+1<len) {
    			a[t+1]='B';
    			s++;
    		}
    		if(a[t+2]!='R'&&t+2<len) {
    			a[t+2]='R';
    			s++;
    		}
    	}
    
    
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    
    	strcpy(a,b);
    	s=0;
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='B'&&t<len) {
    			a[t]='B';
    			s++;
    		}
    		if(a[t+1]!='G'&&t+1<len) {
    			a[t+1]='G';
    			s++;
    		}
    		if(a[t+2]!='R'&&t+2<len) {
    			a[t+2]='R';
    			s++;
    		}
    	}
    
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    	strcpy(a,b);
    	s=0;
    	for(int t=0; t<len; t+=3) {
    		if(a[t]!='B'&&t<len) {
    			a[t]='B';
    			s++;
    		}
    		if(a[t+1]!='R'&&t+1<len) {
    			a[t+1]='R';
    			s++;
    		}
    		if(a[t+2]!='G'&&t+2<len) {
    			a[t+2]='G';
    			s++;
    		}
    	}
    	if(s<maxn) {
    		maxn=s;
    		strcpy(c,a);
    
    	}
    
    	cout<<maxn<<endl;
    	puts(c);
    	return 0;
    }
  • 相关阅读:
    安卓笔记:Android 界面设计指南——人人都是产品经理就是个玩笑话
    Windows CMD 命令
    安卓开发:dex 文件反编译
    VBA 学习笔记 运算符
    工商管理同等学力申硕全国统一考试资料整理
    VBA 学习笔记 判断语句、循环
    VBA 学习笔记 日期时间函数
    安卓自动领水果福气
    【合集】人大商学院同等学力工商管理
    安卓笔记:进度设计原则和常见错误
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781829.html
Copyright © 2011-2022 走看看