【cf 1108 C】枚举一下排列

1.题目链接。题目大意,给定一个字符串,这些字符串只有三种字符,“RGB”。可以改变一些位置的字符,使得同一种字符之间下标之间的距离是3的倍数。问最小需要改变的数量和最终改变好的字符串。

2.分析一下可知,最后的字符串一定是RGB的某种排列的n倍延申。所以我们枚举一下排列,统计一下哪一种排列吻合的最好即可。

#include <bits/stdc++.h>
using namespace std;
string ans;
string s;
int n;
int main()
{
	cin >> n;
	cin >> s;
	ans = s;
	string a("RGB");
	sort(a.begin(), a.end());
	int curRes = n + 1;
	do
	{
		int beta = 0;
		for (int i = 0; i < n; ++i)
			if (a[i % 3] != s[i])
				++beta;
		if (beta < curRes)
		{
			curRes = beta;
			for (int i = 0; i < n; ++i)
				ans[i] = a[i % 3];
		}

	} while (next_permutation(a.begin(), a.end()));

	cout << curRes << '\n';
	cout << ans << '\n';
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41863129/article/details/89710151