首页 > 其他 > 详细

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

时间:2019-03-31 10:18:10      阅读:185      评论:0      收藏:0      [点我收藏+]
D. Diverse Garland
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 diverse.

A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 11) have distinct colors.

In other words, if the obtained garland is tt then for each ii from 11 to n1n−1 the condition titi+1ti≠ti+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 nn (1n21051≤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.

Output

In the first line of the output print one integer rr — 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 tt of length nn — 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
Copy
9
RBGRRBRGG
output
Copy
2
RBGRGBRGR
input
Copy
8
BBBGBRRR
output
Copy
2
BRBGBRGR
input
Copy
13
BBRRRRGGGGGRR
output
Copy
6
BGRBRBGBGBGRG



这个题目可以用暴力枚举+贪心直接来写,很快,也可以用dp写,不过dp我觉得比较复杂,所以我放弃了。


#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
char s[maxn];
int main()
{
	int n,ans=0;
	cin >> n;
	cin >> s;
	if(n==1)
	{
		printf("0\n%s\n", s);
		return 0;
	}
	int len = strlen(s);
	for(int i=1;i<len;i++)
	{
		if(s[i]==s[i-1])
		{
			ans++;
			s[i] = ‘R‘;
			if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
			s[i] = ‘B‘;
			if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
			s[i] = ‘G‘;
			if(s[i]!=s[i+1]&&s[i]!=s[i-1]) continue;
		}
	}
	printf("%d\n", ans);
	printf("%s\n", s);
	return 0;
}

  



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

原文:https://www.cnblogs.com/EchoZQN/p/10630223.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!