zoukankan      html  css  js  c++  java
  • 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 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
    Copy
    9
    RBGRRBRGG
    
    output
    Copy
    2
    RBGRGBRGR
    
    input
    Copy
    8
    BBBGBRRR
    
    output
    Copy
    2
    BRBGBRGR
    
    input
    Copy
    13
    BBRRRRGGGGGRR
    
    output
    Copy
    6
    BGRBRBGBGBGRG
    

     这道题简单在有三个元素,如果是只有两个元素不会这么简单

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    //#include <xfunctional>
    #define ll long long
    #define mod 998244353
    using namespace std;
    int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    
    int main()
    {
        int n,cnt=0;
        cin >> n;
        string t;
        cin >> t;
        for (int i = 1; i < t.size(); i++)
        {
            set<char> sets = { 'B','G','R' };
            if (t[i] == t[i - 1])
            {
                sets.erase(t[i]);
                if (i + 1 < t.size())
                    sets.erase(t[i + 1]);
                cnt++;
                t[i] = *sets.begin();
            }
        }
        cout << cnt << endl;
        cout << t;
        return 0;
    }
  • 相关阅读:
    Activiti6详细教程
    Nginx 与 Tomcat : 413 Request Entity Too Large(请求实体太大)
    windows下安装pytorch
    vim编辑器常用操作
    el-dialog 里面的组件不刷新问题
    el-dialog 里面的 el-form 重置表单问题
    ElementUI树形表格默认展开
    设计模式之 工厂方法模式
    linux创建一个proc代码示例
    redis的安装及使用
  • 原文地址:https://www.cnblogs.com/dealer/p/12381670.html
Copyright © 2011-2022 走看看