C. Nice Garland
原创 于 2023-09-02 13:49:36 发布 · 粉丝可见 · 385 阅读 · 0 · 0 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/132549775
题目:

样例1:
样例2:
cobol<br/>7<br/>RGBGRBB<br/> |
题意:
题目是要在一个字符它的前面两个和后面两个字符不能与它本身有相同的字符。即 范围在 3 之内的字符串不能有相同的字符。
思路:
由于,我们前面两个和后面两个字符不能与它本身有相同的字符,所以可以得出,它将会是个一直重复相同的一段子串。即 读入的时候它的下标 pos % 3 即可获得答案,又因为它们3不同字符,可以组合成 6 种情况,分别是
1
| string F[6] = {"BGR", "BRG", "GBR", "GRB", "RBG", "RGB"};
|
所以我们枚举一遍所有答案,然后找到最小操作数即可。
代码详解如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <unordered_map> #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() {
int _t = 1;
while (_t--) { solve(); }
return 0; }
|
最后提交:
