zoukankan      html  css  js  c++  java
  • D. Diverse Garland-----CF字符串

    D. Diverse Garland
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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<stdio.h>
    #include<string.h>
    #define maxn 100000000
    char str[maxn];
    int n ,ans=0;
    using namespace std;
    int main()
    {
      cin>>n>>str;
      for(int i=0;i<n-1;i++)
      {
        if(str[i]==str[i+1])
        {
          if(str[i]!='R'&&str[i+2]!='R')
            str[i+1]='R';
          else if(str[i]!='G'&&str[i+2]!='G')
            str[i+1]='G';
          else if(str[i]!='B'&&str[i+2]!='B')
            str[i+1]='B';
          ans++;
    
        }
      }
      cout<<ans<<endl;
      for(int i=0;i<n;i++)
        cout<<str[i];
      cout<<endl;
      return 0;
    
    }
  • 相关阅读:
    Goahead 3.1.0 发布,嵌入式 Web 服务器
    jdao 1.0.2 发布,轻量级的orm工具包
    pythonbitstring 3.1.0 发布
    JavaScript 搜索引擎 lunr.js
    Difeye 1.1.4 版本发布
    Chronon 3.5 发布,支持 Java 7
    性能扩展的那些事儿:一味增加硬件并不能解决响应时间问题
    Eclipse SDK 4.2.2/Equinox 3.8.2 发布
    Linux Kernel 3.8.1 发布
    Armadillo C++ Library 3.800 发布
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10316503.html
Copyright © 2011-2022 走看看