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)的抽象类 ...
随机推荐
- Codeforces 427E Police Patrol
找中间的数,然后从两头取. #include<stdio.h> ; int pos[MAX]; int main() { int n,m,tmp; int i; int pol; long ...
- URAL 1204. Idempotents (扩展欧几里得)
题目链接 题意 : 给你一个同余方程, x*x ≡ x (mod n),让你求出所有的小于n的x. 思路 : 先来看同余的概念 :给定一个正整数m,如果两个整数a和b满足a-b能被m整除,即m|(a ...
- scala冒泡排序
scala冒泡排序: object Maopao { def main(args: Array[String]) { val list = List(1, 23, 432, 10, 23, 42, 3 ...
- 关于JAVA数组的几点注意事项与一些低级错误
1.数组不是集合,它只能保存同种类型的多个原始类型或者对象的引用.数组保存的仅仅是对象的引用,而不是对象本身. 2.数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型 ...
- DataType--时间类型
SQL SERVER 存储时间的方式和存放浮点数的方式类似,存放时按照一定公式算出一个数值,存放到页面,在读取时按照公式求算出时间值,再按照默认或指定的时间格式展示给用户. 如果存放DATETIME数 ...
- web集群时session同步的3种方法
在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,一个登录用户,一会是登录状态,一会又不是 ...
- spring @Async 线程池使用
最近公司项目正逐渐从dubbo向springCloud转型,在本次新开发的需求中,全部使用springcloud进行,在使用时线程池,考虑使用spring封装的线程池,现将本次使用心得及内容记录下来 ...
- windows如何通过端口查看对应程序
今天打开SSR报错,说端口被占用. 打开的软件有点多,又不想重启.就需要找到占用的软件,关闭了即可. 打开cmd,输入netstat -aon能看到所有的使用端口 其中1080端口是预留给SSR使用的 ...
- c语言指针的简单实例
c语言的指针的存在使得c语言对硬件的操控,以及灵活性得到了极大的提高. 但是指针的使用存在着很多难点问题. #include<stdlib.h> #include<stdio.h&g ...
- Jmeter_Beanshell_使用Java处理JSON块
版权声明:本文为博主原创文章,转载请注明出处. [环境] ①Jmeter版本:3.2,JDK:1.8 ②前置条件:将json.jar包置于..\apache-jmeter-3.2\lib\下,并将该j ...