zoukankan      html  css  js  c++  java
  • Nice 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 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
    3
    BRB
    
    Output
    1
    GRB
    
    Input
    7
    RGBGRBB
    
    Output
    3
    RGBRGBR

    题意:简单词汇,可以自行阅读。
    思路:因为题目的限制,导致如果这个字符串的长度大于等于3的时候,一定是RBG这三个字符的某一个排列的循环。
    那么RBG一共最多有6种排列方式,( A(3,3) )
    我们手写出这6种全排列。
    {
    "RGB",
    "RBG",
    "GRB",
    "BRG",
    "GBR",
    "BGR"};
    然后以此每一种排列去和字符串进行匹配,找出让字符串为3字符循环的最小消耗即可,
    中间开一个变量k来记录是哪个排列方式让消耗最小,然后最后输出他的循环节即可。
    细节看代码。
    #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 a[maxn];
    char s[50][50]=
    {
    "RGB",
    "RBG",
    "GRB",
    "BRG",
    "GBR",
    "BGR"};
    int main()
    {
        gg(n);
        scanf("%s",a);
        if(n==1)
        {
            printf("0
    ");
            printf("%s",a);
        }else
        {
            int ans=inf;
            int cnt;
            int k;
            repd(i,0,5)
            {
                cnt=0;
                repd(j,0,n-1)
                {
                    if(a[j]!=s[i][(j%3)])
                    {
                        cnt++;
                    }
                }
                if(cnt<ans)
                {
                    ans=cnt;
                    k=i;
                }
            }
            printf("%d
    ",ans );
            repd(i,0,n-1)
            {
                putchar(s[k][(i%3)]);
            }
            printf("
    ");
        }
    
        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/ 希望所写的文章对您有帮助。
  • 相关阅读:
    不用第三个变量互换两变量的值的两种方法。
    struts2截取字符串
    js控制表格单双行颜色交替显示
    Shell字符串
    Shell数组:shell数组的定义、数组长度
    Shell注释
    Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
    Shell替换:Shell变量替换,命令替换,转义字符
    Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
    Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10313774.html
Copyright © 2011-2022 走看看