• C. Nice Garland


    题目:

     样例1:

    输入
    1. 3
    2. BRB

    输出
    1
    GRB

      样例2:

    输入
    1. 7
    2. RGBGRBB

    输出
    3
    RGBRGBR

    题意:

            题目是要在一个字符它的前面两个和后面两个字符不能与它本身有相同的字符。即 范围在 3 之内的字符串不能有相同的字符。

    思路:

            由于,我们前面两个和后面两个字符不能与它本身有相同的字符,所以可以得出,它将会是个一直重复相同的一段子串。即  读入的时候它的下标  pos % 3 即可获得答案,又因为它们3不同字符,可以组合成 6 种情况,分别是 

    string F[6] = {"BGR", "BRG", "GBR", "GRB", "RBG", "RGB"};

    所以我们枚举一遍所有答案,然后找到最小操作数即可。

    代码详解如下:
    1. #include
    2. #include
    3. #define endl '\n'
    4. #define YES puts("YES")
    5. #define NO puts("NO")
    6. #define umap unordered_map
    7. #pragma GCC optimize(3,"Ofast","inline")
    8. #define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
    9. using namespace std;
    10. const int N = 2e6 + 10;
    11. int n;
    12. // 记录所有情况
    13. string F[6] = {"BGR", "BRG", "GBR", "GRB", "RBG", "RGB"};
    14. string s;
    15. int r[6]; // 记录不同答案结果操作数
    16. int maxs = -1; // 记录最多操作数
    17. inline void solve()
    18. {
    19. cin >> n >> s;
    20. // 开始对比
    21. for (int i = 0; i < n; ++i)
    22. {
    23. for (int j = 0; j < 6; ++j)
    24. {
    25. if (F[j][i % 3] != s[i])
    26. {
    27. r[j]++; // 统计操作数
    28. // 找到最多操作数,方便比较最小操作数
    29. maxs = max(maxs, r[j]);
    30. }
    31. }
    32. }
    33. int str_ans = -1; // 最终答案字符串
    34. int ans_op = maxs + 1; // 最终答案操作数
    35. for (int i = 0; i < 6; ++i)
    36. {
    37. if (ans_op > r[i])
    38. {
    39. ans_op = r[i];
    40. str_ans = i;
    41. }
    42. }
    43. // 输出答案
    44. cout << ans_op << endl;
    45. for (int i = 0; i < n; ++i)
    46. {
    47. putchar(F[str_ans][i % 3]);
    48. }
    49. }
    50. int main()
    51. {
    52. // freopen("a.txt", "r", stdin);
    53. // ___G;
    54. int _t = 1;
    55. // cin >> _t;
    56. while (_t--)
    57. {
    58. solve();
    59. }
    60. return 0;
    61. }

    最后提交:

  • 相关阅读:
    10_ue4进阶_添加倒地和施法动作
    Java双非大二找实习记录
    覆盖率分析汇总
    造轮子之asp.net core identity
    Java项目:SSM网吧计费管理系统
    #力扣:9. 回文数@FDDLC
    SRS流媒体服务器:服务器读取RTMP推流数据
    浅谈Git架构和如何避免代码覆盖的事故
    Python 三维姿态估计+Unity3d 实现 3D 虚拟现实交互游戏
    LeetCode710. 黑名单中的随机数.Random Pick with Blacklist [hash映射][前缀和][二分]
  • 原文地址:https://blog.csdn.net/hacker_51/article/details/132549775