题目链接: Diverse Garland
大致题意:
给出一个长度为n的字符串,要使字符串相邻两个字符不同,最少需要改变几次字符
解题思路:
贪心
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
string s, str;
cin >> n >> s;
int res = 0;
for (int i = 1; i < n; ++i) {
if (s[i - 1] == s[i]) {
if (s[i] != 'R' && s[i + 1] != 'R')s[i] = 'R';
else if (s[i] != 'G' && s[i + 1] != 'G')s[i] = 'G';
else if (s[i] != 'B' && s[i + 1] != 'B')s[i] = 'B';
res++;
}
}
cout << res << endl;
cout << s << endl;
return 0;
}