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

    A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 11) have distinct colors.

    In other words, if the obtained garland is tt then for each ii from 11 to n−1n−1 the condition ti≠ti+1ti≠ti+1 should be satisfied.

    Among all ways to recolor the initial garland to make it diverse 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 diverse garland from the given one.

    In the second line of the output print one string tt of length nn — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.

    Examples

    input

    Copy

    9
    RBGRRBRGG
    

    output

    Copy

    2
    RBGRGBRGR
    

    input

    Copy

    8
    BBBGBRRR
    

    output

    Copy

    2
    BRBGBRGR
    

    input

    Copy

    13
    BBRRRRGGGGGRR
    

    output

    Copy

    6
    BGRBRBGBGBGRG

    思路:根据中间的改

    代码;

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    char a[200005];
    int main()
    {
    	int n;
    	cin>>n;
    	scanf("%s",a);
    	int s=0;
    	for(int t=0;t<n-1;t++)
    	{
    		if(a[t]==a[t+1])
    		{
    			s++;
    			if(a[t]!='R'&&a[t+2]!='R')
    		    {
    		    	a[t+1]='R';
    			}
    			if(a[t]!='G'&&a[t+2]!='G')
    		    {
    		    	a[t+1]='G';
    			}
    			if(a[t]!='B'&&a[t+2]!='B')
    		    {
    		    	a[t+1]='B';
    			}
    		}
    	
    	}
    	
    		cout<<s<<endl;
    		puts(a);
    	
    	return 0;
    } 
  • 相关阅读:
    Silverlight 手鼓达人-仿太鼓达人 开源
    友盟推送 .NET (C#) 服务端 SDK rest api 调用库
    信鸽推送 .NET (C#) 服务端 SDK rest api 调用库(v1.2)
    MachineKey 操作 之 应用集群中SSO应用生成MachineKey
    MachineKey 操作 之 获取 MachineKey
    Visual Studio (VS IDE) 你必须知道的功能和技巧
    正则表达式 匹配(获取) 所有表名
    程序员何苦为难程序员!
    学习资料集合
    转载:centos7 yum安装php7.3(解决yum安装apache关联不了PHP的问题)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781828.html
Copyright © 2011-2022 走看看