Codeforces Round #535 (Div. 3) C. Nice Garland(暴力)

版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/86623570

题目链接:http://codeforces.com/contest/1108/problem/C

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

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


AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
	int len;
	cin>>len;
	string str;
	cin>>str;
	string a[6] = {"RGB", "RBG", "GRB", "GBR", "BRG","BGR"};
	int ans = 0x3f3f3f3f , xx = -1;
	for(int i=0;i<6;i++){
		int cnt = 0;
		for(int j=0;j<len;j++){
			if(a[i][j%3] != str[j]) cnt ++;
		}
		if(cnt < ans){
			ans = cnt;
			xx = i;
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<len;i++){
		cout<<a[xx][i%3];
	}
	puts("");
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Charles_Zaqdt/article/details/86623570