题目链接: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. void java.lang.System.gc()

    void java.lang.System.gc() Runs the garbage collector. Calling the gc method suggests that the Java ...

  2. 自动化测试中CSS SELECTOR选择器的一些写法

    常见符号: #表示id .表示class >表示子元素,层级 一个空格也表示子元素,但是是所有的后代子元素,相当于xpath中的相对路径 #input 选择id为input的节点 .Volvo ...

  3. 12C -- ORA-65005: missing or invalid file name pattern for file

    克隆pdb创建新的pdb的时候遇到了以下的错误,具体过程如下文.数据库版本:12.2.0.1 查看已有pdb的tempfile文件 SQL> select name from v$tempfil ...

  4. Effective Java 第三版——56. 为所有已公开的API元素编写文档注释

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  5. Intro to Jedis – the Java Redis Client Library

    转自:http://www.baeldung.com/jedis-java-redis-client-library 1. Overview This article is an introducti ...

  6. [svc]证书各个字段的含义

    证书生成工具 1,openssl 2,jdk自带的keystone 3,cfssl 证书中各个字段的含义 - 查看证书的内容 openssl x509 -in /etc/pki/CA/cacert.p ...

  7. FFmpeg: AVFormatContext 结构体分析

    AVFormatContext 结构体分析这个结构体描述了一个媒体文件或媒体流的构成和基本信息.这是FFMpeg中最为基本的一个结构,是其他所有结构的根,是一个多媒体文件或流的根本抽象.主要成员释义: ...

  8. linux每日命令(27):chmod命令

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...

  9. Ansible and FileBeta

    使用Ansible通过ssh批量读取和修改Client设备 1. 安装ansible工具 apt-get install ansible 2. 添加需要访问的client信息 ansible配置文件如 ...

  10. 【原】使用Json作为Python和C#混合编程时对象转换的中间文件

    一.Python中自定义类对象json字符串化的步骤[1]   1. 用 json 或者simplejson 就可以: 2.定义转换函数: 3. 定义类 4. 生成对象 5.dumps执行,引入转换函 ...