题目链接:http://codeforces.com/problemset/problem/1108/D

time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the i-th lamp is si ('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 1) have distinct colors.

In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i≠t_{i+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 $n (1≤n≤2⋅10^5)$ — the number of lamps.

The second line of the input contains the string s consisting of n characters 'R', 'G' and 'B' — colors of lamps in the garland.

Output
In the first line of the output print one integer r — 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 t of length n — 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
9
RBGRRBRGG

output
2
RBGRGBRGR

input
8
BBBGBRRR

output
2
BRBGBRGR

input
13
BBRRRRGGGGGRR

output
6
BGRBRBGBGBGRG

题意:

$n$ 个灯笼排成一排,每个灯笼初始颜色可能为 $R(ed),G(reen),B(lue)$ 其中的一种,现在要求你尽量少地改变一些灯笼的颜色,使得这一排的灯笼中任意相邻的两个颜色都不相同,我们称这样的一排灯笼为“多样化的”。

题解:

令 $f[i][0,1,2]$ 表示前 $i$ 个灯笼,第 $i$ 个的颜色为 $0$ 或者 $1$ 或者 $2$ 时,最少要改变多少个灯笼的颜色才能使这前 $i$ 个灯笼“多样化的”。

转移方程很简单: dp[i][x] = min(dp[i][x],dp[i-][y]+(c[i]!=x)) ,且满足 $x!=y$。

另外唯一需要注意的是,如果 $dp[i][x]$ 更新了,就记录一下前一个灯笼的颜色。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
int n,c[maxn];
int dp[maxn][];
int pre[maxn][];
char ch[]={'R','G','B'};
int main()
{
cin>>n;
string s; cin>>s;
for(int i=;i<=s.size();i++)
{
if(s[i-]=='R') c[i]=;
if(s[i-]=='G') c[i]=;
if(s[i-]=='B') c[i]=;
} memset(dp,0x3f,sizeof(dp));
dp[][]=(c[]!=), dp[][]=(c[]!=), dp[][]=(c[]!=);
for(int i=;i<=n;i++)
{
for(int x=;x<;x++)
{
for(int y=;y<;y++)
{
if(x==y) continue;
if(dp[i][x]>dp[i-][y]+(c[i]!=x))
{
dp[i][x]=dp[i-][y]+(c[i]!=x);
pre[i][x]=y;
}
}
}
} int ans=0x3f3f3f3f,color;
for(int k=;k<;k++) if(ans>dp[n][k]) ans=dp[n][k], color=k; cout<<ans<<endl;
s.clear();
for(int i=n;i>=;i--) s=ch[color]+s, color=pre[i][color];
cout<<s<<endl;
}

Codeforces 1108D - Diverse Garland - [简单DP]的更多相关文章

  1. Codeforces - 702A - Maximum Increase - 简单dp

    DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...

  2. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

  3. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  4. Codeforces - 909C - Python Indentation - 简单dp

    http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...

  5. CodeForces 30C Shooting Gallery 简单dp

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...

  6. Codeforces - 1033C - Permutation Game - 简单dp - 简单数论

    https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...

  7. CodeForces 22B Bargaining Table 简单DP

    题目很好理解,问你的是在所给的图中周长最长的矩形是多长嗯用坐标(x1, y1, x2, y2)表示一个矩形,暴力图中所有矩形易得递推式:(x1, y1, x2, y2)为矩形的充要条件为: (x1, ...

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

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

  9. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

随机推荐

  1. JAVA 自定义注解在自动化测试中的使用

    在UI自动化测试中,相信很多人都喜欢用所谓的PO模式,其中的P,也就是page的意思,于是乎,在脚本里,或者在其它的page里,会要new很多的page对象,这样很麻烦,前面我们也讲到了注解的使用,很 ...

  2. iostat查看linux硬盘IO性能

    rrqm/s:   每秒进行 merge 的读操作数目.即 delta(rmerge)/swrqm/s:  每秒进行 merge 的写操作数目.即 delta(wmerge)/sr/s:        ...

  3. packetfence 7.2网络准入部署(一)

    packetfence 是一款内网准入软件,刚开始研究的时候也是一脸懵逼,资料少的可怜,前后玩了几个月,中途很多次都想放弃了,填完了很多坑,最后也算是成功了 好了,今天就讲一下packetfence所 ...

  4. 【iCore4 双核心板_FPGA】例程二:GPIO输入实验——识别按键输入

    实验现象: 按键每按下一次,三色LED切换一次状态. 核心源代码: module key_ctrl( input clk_25m, input rst_n, input key, output fpg ...

  5. vue用组件构建应用

    组件系统是 Vue.js 另一个重要概念,因为它提供了一种抽象,让我们可以用独立可复用的小组件来构建大型应用.如果我们考虑到这点,几乎任意类型的应用的界面都可以抽象为一个组件树: 在 Vue 里,一个 ...

  6. volatile内存语义

    全面理解Java内存模型(JMM)及volatile关键字 volatile的内存语义 Volatile读写所建立的happens-before关系Volatile读写的内存语义 锁: 获取和释放Vo ...

  7. windows python3.7安装numpy问题的解决方法

    我的是win7的系统,去python官网下载python3.7安装 CMD  #打开命令窗口 pip install numpy #在cmd中输入 提示 需要c++14.0, 解决办法: 1, 进入h ...

  8. [IR] Dictionary Coding

    [数据压缩]LZ77算法原理及实现 [数据压缩]LZ78算法原理及实现 Lempel–Ziv–Welch 年发表的论文<A Universal Algorithm for Sequential ...

  9. [Unity3D] 04 - Event Manager

    message消息管理 脚本与GameObject的关系 被显式添加到 Hierarchy 中的 GameObject 会被最先实例化,GameObject 被实例化的顺序是从下往上. GameObj ...

  10. PHP实现删除非站内外部链接实例代码

    /** *  删除非站内链接 * * @access    public * @param     string  $body  内容 * @param     array  $allow_urls  ...