C. Nice Garland

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 si ('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=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 y.

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

题目描述:

给出由R,G,B组成的字符串,要求每个相同的字母字符串下标满足d=|i-j|可以被3整除。求出最小修改多少个,并打印出修改后的结果。

分析:

因为开头的3个字母一定要是不相同的,而且后面的字母都是取决于前面3个的。

比如开头是RGB则下标为0,3,6,9(n%3==0)的就一定要相同,此例就全为R。

所以只要a[i+3](i=0,1,2)相同即可。

所以把开头6种情况(RGB不同的排序)全列出来,取最小那种即为答案。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
int ans;
char a[200007];
pair <char,int> p[3][3];
void hc()
{
    for(int i=0;i<3;i++)
    {
        char ch=a[i];
        //修改 
        for(int j=i;j<n;j+=3)
        {
            if(a[j]!=ch)
            {
                a[j]=ch;
                ans++;
            }
        }
    }
 } 
int main()
{
    cin>>n;
    getchar();
    for(int i=0;i<n;i++)
    {
        a[i]=getchar();
    }
    int m;
    if(n==2)
    {
        if(a[0]==a[1])
        {
            ans=1;
            if(a[0]!='G')
            {
                a[1]='G';
            }
            else if(a[0]!='R')
            {
                a[1]='R';
            }
            else a[1]='B';
        }
    }
    else if(n==3)
    {
        if(a[0]==a[1]&&a[1]==a[0])
        {
            ans=2;
            if(a[0]=='G')
            {
                a[1]='B';
                a[2]='R';
            }
            else if(a[0]=='R')
            {
                a[1]='B';
                a[2]='G';
            }
            else 
            {
                a[1]='G';
                a[2]='R';
            }
        }
        if(a[0]==a[1])
        {
            if(a[2]!='G'&&a[0]!='G')
            {
                a[1]='G';    
            }
            else if(a[2]!='R'&&a[0]!='R')
            {
                a[1]='R';
            }
            else
            {
                a[1]='B';
            }
            ans=1;
        }
        else if(a[0]==a[2])
        {
            if(a[1]!='G'&&a[0]!='G')
            {
                a[2]='G';    
            }
            else if(a[1]!='R'&&a[0]!='R')
            {
                a[2]='R';
            }
            else
            {
                a[2]='B';
            }
            ans=1;
        }
        else if(a[2]==a[1])
        {
            if(a[0]!='G'&&a[2]!='G')
            {
                a[1]='G';    
            }
            else if(a[0]!='R'&&a[2]!='R')
            {
                a[1]='R';
            }
            else
            {
                a[1]='B';
            }
            ans=1;
        }
    }
    else if(n>3)
    {    
        for(int i=0;i<3;i++)
        {
            p[i][0].first='R';
            p[i][0].second=0;
            for(int j=i;j<n;j+=3)
            {
                if(a[j]!=p[i][0].first)
                {
                    p[i][0].second++;
                }
            }
            p[i][1].first='G';
            p[i][1].second=0;
            for(int j=i;j<n;j+=3)
            {
                if(a[j]!=p[i][1].first)
                {
                    p[i][1].second++;
                }
            }
            p[i][2].first='B';
            p[i][2].second=0;
            for(int j=i;j<n;j+=3)
            {
                if(a[j]!=p[i][2].first)
                {
                    p[i][2].second++;
                }
            }
        }
        int Rank[6]={0};
        Rank[0]=p[0][0].second+p[1][2].second+p[2][1].second;
        Rank[1]=p[0][0].second+p[2][2].second+p[1][1].second;
        Rank[2]=p[1][0].second+p[0][2].second+p[2][1].second;
        Rank[3]=p[2][0].second+p[0][2].second+p[1][1].second;
        Rank[4]=p[2][0].second+p[1][2].second+p[0][1].second;
        Rank[5]=p[1][0].second+p[2][2].second+p[0][1].second;
 
        int k=0;
        for(int i=0;i<6;i++)
        {
            if(Rank[i]<Rank[k])
            {
                k=i;
            }
        }
        switch(k)
        {
            case 0:a[0]='R',a[1]='B',a[2]='G';break;
            case 1:a[0]='R',a[1]='G',a[2]='B';break;
            case 2:a[0]='B',a[1]='R',a[2]='G';break;
            case 3:a[0]='B',a[1]='G',a[2]='R';break;
            case 4:a[0]='G',a[1]='B',a[2]='R';break;
            case 5:a[0]='G',a[1]='R',a[2]='B';break;
        }
        hc();
    } 
    printf("%d\n",ans);
    for(int i=0;i<n;i++)
    printf("%c",a[i]);
    return 0;
}

(代码有误,请勿模仿)

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12209016.html