Nice Garland CodeForces - 1108C(思维)

 题意是给了一个长度为n的字符串,且字符串只包含'R','B','G'三种字符,可以改变任何一个字符,使得任意两个相同的字符的距离是3的倍数,输出改动最少的操作且输出改动后的字符串。

 任意两个相同的字符的距离是3的倍数,换种说法其实就是RGBRGB这样的三个一循环,所以我们就把RGB的所有排列方式列出来然后暴力枚举找到改动最小的操作就好了。

扩展一下:n个字符距离是n的倍数,这一定是全排列的循环

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int main(){
	int len;
	cin>>len;
	string s;
	cin>>s;
	string a[6]={"RBG","BRG","BGR","RGB","GRB","GBR"};
	int ans=inf,xx=-1;
	for(int i=0;i<6;i++){
		int cnt=0;
		for(int j=0;j<len;j++){
			if(a[i][j%3]!=s[j])cnt++;
		}
		if(cnt<ans){
			ans=cnt;
			xx=i;
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<len;i++)
	cout<<a[xx][i%3];
	cout<<endl;
}
原创文章 38 获赞 7 访问量 1739

猜你喜欢

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