
|
| 1 GRB |
|
| 3 RGBRGBR |
题目是要在一个字符它的前面两个和后面两个字符不能与它本身有相同的字符。即 范围在 3 之内的字符串不能有相同的字符。
由于,我们前面两个和后面两个字符不能与它本身有相同的字符,所以可以得出,它将会是个一直重复相同的一段子串。即 读入的时候它的下标 pos % 3 即可获得答案,又因为它们3不同字符,可以组合成 6 种情况,分别是
string F[6] = {"BGR", "BRG", "GBR", "GRB", "RBG", "RGB"};
所以我们枚举一遍所有答案,然后找到最小操作数即可。
- #include
- #include
- #define endl '\n'
- #define YES puts("YES")
- #define NO puts("NO")
- #define umap unordered_map
- #pragma GCC optimize(3,"Ofast","inline")
- #define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
- using namespace std;
- const int N = 2e6 + 10;
- int n;
-
- // 记录所有情况
- string F[6] = {"BGR", "BRG", "GBR", "GRB", "RBG", "RGB"};
- string s;
- int r[6]; // 记录不同答案结果操作数
- int maxs = -1; // 记录最多操作数
- inline void solve()
- {
- cin >> n >> s;
- // 开始对比
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < 6; ++j)
- {
- if (F[j][i % 3] != s[i])
- {
- r[j]++; // 统计操作数
-
- // 找到最多操作数,方便比较最小操作数
- maxs = max(maxs, r[j]);
- }
- }
- }
-
- int str_ans = -1; // 最终答案字符串
- int ans_op = maxs + 1; // 最终答案操作数
- for (int i = 0; i < 6; ++i)
- {
- if (ans_op > r[i])
- {
- ans_op = r[i];
- str_ans = i;
- }
- }
-
- // 输出答案
- cout << ans_op << endl;
- for (int i = 0; i < n; ++i)
- {
- putchar(F[str_ans][i % 3]);
- }
- }
-
-
- int main()
- {
- // freopen("a.txt", "r", stdin);
- // ___G;
- int _t = 1;
- // cin >> _t;
- while (_t--)
- {
- solve();
- }
-
- return 0;
- }
