Codeforces - 1108C. Nice Garland & 1108D. Diverse Garland(排列 | 枚举 )

Codeforces - 1108C. Nice Garland & 1108D. Diverse Garland(排列 | 枚举 )


Codeforces - 1108C. Nice Garland

题目链接
题目

给你n个有n个字符的字符串,Nice Garland定义为任意两个位置i、j的字符,如果|i - j| % 3 == 0 && str[i].color = str[j].color,这个串就是Nice Garland,给你一个字符串,要你改变最少的字符,重新组装字符串,使得字符串是Nice Garland
在这里插入图片描述

解析

使用枚举{'R', 'G', 'B'}三个字符的排列,然后每次遍历数组,去组合字符串,看每次需要改变多少字符,取最小需要改变的一个即可。
在这里插入图片描述

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args){
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        PrintStream out = System.out;
        int n = cin.nextInt();
        String str = cin.next();
        char[] chs = str.toCharArray();
        char[] initArr = {'R', 'G', 'B'};
        char[] tmpArr = new char[3];
        char[] resPer = new char[3];
        int res = Integer.MAX_VALUE;
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                for(int k = 0; k < 3; k++){
                    if(i == j || j == k || i == k)
                        continue;
                    tmpArr[0] = initArr[i];
                    tmpArr[1] = initArr[j];
                    tmpArr[2] = initArr[k];
                    chs = str.toCharArray(); //注意每次都要用原来的str
                    int count = 0;
                    for(int p = 0; p < n; p++){
                        if(chs[p] != tmpArr[p%3]){
                            count++;
                            chs[p] = tmpArr[p%3];
                        }
                    }
                    if(count < res){
                        res = count;
                        for(int p = 0; p < 3; p++)
                            resPer[p] = tmpArr[p];
                    }
                }
            }
        }
        out.println(res);
        for(int i = 0; i < n; i++)
            out.print(resPer[i%3]);
        out.println();
    }
}

Codeforces - 1108D. Diverse Garland

题目链接
题目

这题和上题差不多,也是给你n个有n个字符的字符串,这里的Diverse Garland定义为任意两个相邻的字符不能相等,要你改变最少的字符,使得字符串是Diverse Garland
在这里插入图片描述

解析

使用贪心+枚举即可:

  • 1 ~ n-1位置枚举,如果str[i] == str[i-1],说明至少需要改变其中一个;
  • 此时我们去看str[i + 1]的情况,此时贪心的思想就是让str[i-1] 、str[i]、str[i+1]这三个字符,只需要改变str[i]就能让str[i-1]、str[i]、str[i+1]三个字符组成的字符串是Diverse Garland
  • 具体的六种情况我在代码中都注释了,注意细节即可;
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        PrintStream out = System.out;
        int n = cin.nextInt();
        String str = cin.next();
        char[] chs = str.toCharArray();
        int count = 0;
        for (int i = 1; i <= n - 1; i++) {
            if (chs[i] != chs[i - 1])
                continue;
            count++;
            if (chs[i] == 'R') {  // RRB -> RGB
                if (i + 1 < n && chs[i + 1] == 'B') {
                    chs[i] = 'G';
                } else { // RR(R|G) -> RB(R|G)
                    chs[i] = 'B';
                }
            } else if (chs[i] == 'B') {
                if (i + 1 < n && chs[i + 1] == 'R') { //BBR --> BGR
                    chs[i] = 'G';
                } else {  // BB(B|G) --> BR(B|G)
                    chs[i] = 'R';
                }
            } else { // chs[i] == 'G'
                if (i + 1 < n && chs[i + 1] == 'R') { //GGR --> GBR
                    chs[i] = 'B';
                } else {             // GG(G|B) --> GR(G|B)
                    chs[i] = 'R';
                }
            }

        }
        out.println(count);
        out.println(new String(chs));
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值