C. Nice Garland-------字符串
1 second
256 megabytes
standard input
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 nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is tt, then for each i,ji,j such that ti=tjti=tj should be satisfied |i−j| mod 3=0|i−j| mod 3=0. The value |x||x| means absolute value of xx, the operation x mod yx mod y means remainder of xx when divided by yy.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
The first line of the input contains one integer nn (1≤n≤2⋅1051≤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.
In the first line of the output print one integer rr — the minimum number of recolors needed to obtain a nice garland from the given one.
In the second line of the output print one string tt of length nn — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
3
BRB
1
GRB
7
RGBGRBB
3
RGBRGBR next_permutation(a, a + 3)
输出数组a的字典序的全排列。 eg
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int ans[]={,,,};
sort(ans,ans+); /* 这个sort可以不用,因为{1,2,3,4}已经排好序*/
do /*注意这步,如果是while循环,则需要提前输出*/
{
for(int i=;i<;++i)
cout<<ans[i]<<" ";
cout<<endl;
}while(next_permutation(ans,ans+));
return ;
}
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<stdio.h>
#include<math.h>
using namespace std;
#define ll long long
using namespace std;
const int maxn = 2e5 + ;
char str[maxn], s1[maxn], s2[maxn];
int n, a[]; int main()
{
cin >> n >> str;
map<int, char> m;
m[] = 'R';
m[] = 'G';
m[] = 'B';
int ans = maxn * ;
a[] = , a[] = ;
do
{
s1[] = m[a[]];
s1[] = m[a[]];
s1[] = m[a[]];
for (int q = ; q < n; q++)
{
s1[q] = s1[q - ];
}
int cnt = ;
for (int q = ; q < n; q++)
{
if (s1[q] != str[q])
cnt++;
}
if (cnt < ans)
{
ans = cnt;
strcpy(s2, s1);
}
} while (next_permutation(a, a + ));
printf("%d\n", ans);
for (int i = ; i < n; i++)
printf("%c", s2[i]);
return ;
}
C. Nice Garland-------字符串的更多相关文章
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Nice Garland CodeForces - 1108C (思维+暴力)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- 1.23 codeforces div3 C.Nice Garland
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Codeforces758B Blown Garland 2017-01-20 10:19 87人阅读 评论(0) 收藏
B. Blown Garland time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...
- java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...
- JavaScript 字符串实用常操纪要
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Redis的简单动态字符串实现
Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...
随机推荐
- 19、SOAP安装,运用与比对结果解释
转载:http://www.dengfeilong.com/post/Soap2.html https://blog.csdn.net/zhu_si_tao/article/details/71108 ...
- Yii2验证登录得User类
Yii2中的 Class yii\web\User 是如果进行验证登录,如果我们使用User类验证登录会给我们减少很多麻烦.在此就拿Yii2中自带的登录功能进行说明. 配置.在应用配置文件compo ...
- rest 参数和扩展运算符
rest 参数和扩展运算符 rest 参数的形式为 ...变量名:扩展运算符是三个点 .... rest 参数 function add(...values) { console.log(values ...
- Appium移动端自动化测试之元素定位(三)
1.name定位 driver.find_element_by_id(') driver.find_element_by_id(') driver.find_element_by_name('登录') ...
- 【Android学习】Android工程资源命名禁忌
在制作一个继续按钮时,将button的id设置为continue,发现报了错误,error: invalid symbol: 'continue' 一开始还以为是编码问题,后来百度之后才知道安卓And ...
- 激光样式——第九届蓝桥杯C语言B组(国赛)第二题
原创 标题:激光样式x星球的盛大节日为增加气氛,用30台机光器一字排开,向太空中打出光柱.安装调试的时候才发现,不知什么原因,相邻的两台激光器不能同时打开!国王很想知道,在目前这种bug存在的情况下, ...
- wcf文件上传时碰到的配置问题
1.远程服务器返回了意外相应:(413) Request Entity Too Large 修改客户端配置maxReceivedMessageSize="2147483647" & ...
- TSQL--删除正在运行的数据库
); SET @dbName='DB1_SNAP' BEGIN TRY --===================================== --查找当前数据库所有连接并删除 DECLARE ...
- 合并datagridview 条件合并行数据
public void HeBing() { int rowsCount; int CellCount; rowsCount = FG1.Rows.Count; CellCount = FG1.Col ...
- SQL计算时间差并排除周末
SQL计算时间差并排除周末 CREATE FUNCTION DI_FN_GET_WorkDay (@begin DATETIME , @end DATETIME ) RETURNS int BEGIN ...