Diverse Garland CodeForces - 1108D(思维)

题意:现在给定一个由R、G、B三个字母组成的字符串,每次操作可将任意一个位置的字符换成任意一个{ R, G, B }中的字符,问最少进行多少次操作,能使得此字符串任意位置的字符不与其左右位置的字符相同。同时输出改后的字符串。

题解:首先如果相邻的相同我肯定要改一个,那我肯定能改哪个就改哪个只要改后不矛盾就行,这其实是一个动态的过程,一遍修改一边判断。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char a[200005];
int main()
{
	int n;
	cin>>n;
	scanf("%s",a);
	int s=0;
	for(int t=0; t<n-1; t++)
	{
		if(a[t]==a[t+1])
		{
			s++;
			if(a[t]!='R'&&a[t+2]!='R')a[t+1]='R';
			if(a[t]!='G'&&a[t+2]!='G')a[t+1]='G';
			if(a[t]!='B'&&a[t+2]!='B')a[t+1]='B';
		}
	}
	cout<<s<<endl;
	puts(a);
	return 0;
}
原创文章 38 获赞 7 访问量 1738

猜你喜欢

转载自blog.csdn.net/Alanrookie/article/details/106098057