题意:灯有三种颜色R,G,B。只要同一种颜色相邻就不可以。问最少需要换几次,可以使在一串灯中没有相邻灯的颜色相同。

思路:贪心思路:我们知道一个程序都是要子阶段,然后子阶段各个组合起来形成这个程序。那么对于每一个子阶段的贪心,就能形成整个程序的贪心。

贪心思路就是:只要出现相邻的相同a[i]==a[i+1], 然后,就选择一个a[i+1]!=a[i]&&a[i+1]!=a[i+2]的情况。注意:最后2个字母相同时,需要自己处理一下。

#include<iostream>
using namespace std;
const int maxn = 2e5 + ;
int main(){
int n; char st[maxn];
cin >> n; cin >> st;
if (n < ){
cout << << endl;
cout << st << endl;
}
else{
int ans = ;
for (int i = ; i < n - ; ++i){
if (st[i] == st[i + ]&&i<n-){
if (st[i] == 'G'&&st[i + ] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'G'&&st[i + ] == 'R'){ st[i + ] = 'B'; }
else if (st[i] == 'G'&&st[i + ] == 'B'){ st[i + ] = 'R'; }
else if (st[i] == 'B'&&st[i + ] == 'B'){ st[i + ] = 'R'; }
else if (st[i] == 'B'&&st[i + ] == 'R'){ st[i + ] = 'G'; }
else if (st[i] == 'B'&&st[i + ] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'R'&&st[i + ] == 'R'){ st[i + ] = 'B'; }
else if (st[i] == 'R'&&st[i + ] == 'G'){ st[i + ] = 'B'; }
else if (st[i] == 'R'&&st[i + ] == 'B'){ st[i + ] = 'G'; }
ans++;
}
else if (i == n - && st[i] == st[i + ]){
// cout << st[i] << st[i + 1] << endl;
if (st[i] == 'B'){ st[i + ] = 'G'; }
else if (st[i] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'R'){ st[i + ] = 'G'; }
ans++;
}
}
cout << ans << endl;
cout << st << endl;
}
}

D. Diverse Garland的更多相关文章

  1. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

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

  2. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  3. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  4. D. Diverse Garland-----CF字符串

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

  5. Codeforces-D-Diverse Garland(思维)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  6. Codeforces Round #535 (Div. 3) 解题报告

    CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...

  7. CF刷刷水题找自信1

    CF 1108A Two distinct points 题目意思:给你两个线段的起点和终点,让你给出两个不同的点,这两点分别处于两个不同的线段之中.解题思路:题目说如果存在多种可能的点,随意一组答案 ...

  8. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  9. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...

随机推荐

  1. Verification and validation

    Verification Verification is the process to make sure the product satisfies the conditions imposed a ...

  2. 4. 泛型_EJ

    第23条: 不要在新代码中使用原生态类型 声明中具有一个或多个类型参数的类或接口,就是泛型类或接口.每种泛型都定义一组参数化的类型,每个泛型都定义一个原生态类型.例如List<E>相对应的 ...

  3. 你所不知道的JSON.stringify

    译者按: 老司机们,你知道JSON.stringify还有第二个和第三个可选参数吗?它们是什么呢? 原文: What you didn’t know about JSON.Stringify 译者: ...

  4. 人类阅读的优越方式打印php数组

    在程序开发过程中:打印数据进行查看调试是非常频繁的:如果没有一种易于阅读的样式那是相当痛苦的:先定义一个数组: $array=array( 't0'=>'test0', 't1'=>'te ...

  5. vue2+webpack 移动生态 常用依赖

    1.脚手架:官方的vue-cli已经很方便了 2.路由:vue-router : https://router.vuejs.org/zh-cn/essentials/named-routes.html ...

  6. angular select 默认值

    <select ng-model="selected" ng-options="x.id as x.name for x in users">< ...

  7. es6 语法 (解构赋值)

    1.结构赋值 { let a,b,c; [a,b] = [1,2]; console.log(a,b); //1,2} { let a,b,rest; [a,b,...rest] = [1,2,3,4 ...

  8. Python 实现批量从不同的Linux服务器下载文件

    基于Python实现批量从不同的Linux服务器下载文件   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/file_for_downl ...

  9. Android为TV端助力 关于Fragment你所需知道的一切!

    转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...

  10. LeetCode单排日记

    初衷 之前有研究过一段时间数据结构与算法,但平时使用的不多,就连排序都很少用(自从JDK8有了Stream,就再也没有手写排序了.),所谓用进废退,时至今日,能记住的已经不多了,还记得之前有一次面试, ...