原题
题目大意
和上一题差别不大,就是这次和旁边不同的就行
题目分析
将上一题的贪心代码复制过来改改就行……不过这次贪心是看前一位和后一位(话说能叫贪心么)
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>char ori[400010];
bool been[128],flag;
int ans = 0,fs = 0,ft = 0,tot = 0;
int main()
{int n;char t;scanf("%d",&n);scanf("%s",ori);flag = false;for (ft = 0;ft < 2 && ft < n;ft++){if (!been[ori[ft]]){been[ori[ft]] = true;}else{flag = true;}}if (flag) {been[ori[ft]] = true;if (!been['R']){ori[1] = 'R';been['R'] = true;ans++;}else if (!been['B']){ori[1] = 'B';been['B'] = true;ans++;}else if (!been['G']){ori[1] = 'G';been['G'] = true;ans++;}been[ori[ft]] = false;}
// been[ori[fs]] = false;while (ft < n){been[ori[fs++]] = false;flag = false;if (been[ori[ft]]) {flag = true;}else been[ori[ft]] = true;ft++;if (flag) {been[ori[ft]] = true;if (!been['R']){ori[fs + 1] = 'R';been['R'] = true;ans++;}else if (!been['B']){ori[fs + 1] = 'B';been['B'] = true;ans++;}else if (!been['G']){ori[fs + 1] = 'G';been['G'] = true;ans++;}been[ori[ft]] = false; }}ori[ft] = '\0';printf("%d\n%s",ans,ori);return 0;
}