题意大致就是给你一个字符串序列,这个字符串是一个手环的展开,其中规定跟当前珠子距离2个单位的珠子跟当前位颜色相同的手环是好手环,现在给你一个手环,请你改变其中的珠子颜色使其成为好手环,颜色只有红黄蓝三种。
分析:从题意可以看出,要求每相隔两个单位的珠子颜色要求一样,则表明是把手环分成了三个珠子为一个段的分段和;
那么在处理这道题时就可以采用一个暴力模拟的过程,自己先设定一个颜色序,因为下面要采用next_permutation()函数,所以在开始时设定对比组的颜色是BGR,然后依次然给定的手环上的珠子和对比组进行比较,然后计算最小的颜色不同次数;
代码:
#include<bits/stdc++.h>
using namespace std;
void solve()
{
string s;
int n;
cin >> n >> s;
string T, R;
T = "BGR";
int maxn = 0x3f3f3f3f;
do
{
int time = 0;
for (int i = 0; i < n; i++)
{
if (s[i] != T[i % 3]) time++;
}
if (time < maxn)
{
maxn = time;
R = "";
for (int i = 0; i < n; i++) R.push_back(T[i % 3]);
}
} while (next_permutation(T.begin(), T.end()));
cout << maxn << endl;
//printf("%d\n", time);
cout << R << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
//system("pause");
return 0;
}