zoukankan      html  css  js  c++  java
  • Diverse Garland CodeForces

    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 n1n−1 the condition titi+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 (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 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
    9
    RBGRRBRGG
    
    Output
    2
    RBGRGBRGR
    
    Input
    8
    BBBGBRRR
    
    Output
    2
    BRBGBRGR
    
    Input
    13
    BBRRRRGGGGGRR
    
    Output
    6
    BGRBRBGBGBGRG

    题意:给定一个字符串,只包含RGB三个字符,你可以改变某些字符使之这个字符串相邻的字符不相等。
    那么我们只需要枚举从第二个字符开始的每一个字符串,判定是否和前面的字符相等,如果相等就改成不和后面字串相等字符,这样消耗就一定最小。
    很水的一题,细节看code。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int n;
    char s[maxn];
    int main()
    {
        gg(n);
        scanf("%s",s);
        int ans=0;
        repd(i,1,n-1)
        {
            if(s[i]==s[i-1])
            {
                if(s[i]=='B')
                {
                    if(s[i+1]!='R')
                    {
                        s[i]='R';
                    }else
                    {
                        s[i]='G';
                    }
                }else if(s[i]=='R')
                {
                    if(s[i+1]!='B')
                    {
                        s[i]='B';
                    }else
                    {
                        s[i]='G';
                    }
                }else if(s[i]=='G')
                {
    
                    if(s[i+1]!='R')
                    {
                        s[i]='R';
                    }else
                    {
                        s[i]='B';
                    }
                }
                ans++;
            }
    
        }
        printf("%d
    ",ans );
        printf("%s
    ", s);
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    开发实践思考(一)
    记一次前端适配后台接口改造的开发小结
    百度地图AK申请
    Idea 不更新pom.xml中的jar包
    java junit @Test 变量共享问题 --springboot 中的Controller静态变量可以共享
    Java如何对HashMap按值进行排序--非String int 类型时
    Salesforce Integration 概览(七) Data Virtualization数据可视化
    Salesforce Integration 概览(六) UI Update Based on Data Changes(UI自动更新基于数据变更)
    Salesforce Integration 概览(五) Remote Call-In(远程操作 外部->salesforce)
    Salesforce Integration 概览(四) Batch Data Synchronization(批量数据的同步)
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10313779.html
Copyright © 2011-2022 走看看