Advertisement
am1x

Untitled

Aug 20th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <algorithm>
  6.  
  7. typedef unsigned int uint_t;
  8. const uint_t N = 5000000, Z = 26, ZA = 32;
  9.  
  10. static uint8_t as[N + 1];
  11.  
  12. int main()
  13. {
  14. uint_t n = 0, k = 0;
  15. int st = scanf("%u%u", &n, &k);
  16. assert (st == 2);
  17. assert (0 < n && n <= N);
  18. assert (k <= N);
  19. st = scanf("%5000000s", as);
  20. assert (st == 1);
  21. as[N] = 0;
  22. assert (strlen((const char *) as) == n);
  23. // fprintf(stderr, "n=%u, k=%u, as='%s' \n", n, k, as);
  24. for (uint_t i = 0; i < n; i++) {
  25. uint_t x = as[i] - 'a';
  26. assert (x < Z);
  27. as[i] = x;
  28. }
  29.  
  30. uint_t cs[2][ZA];
  31. for (uint_t i = 0; i < 2; i++) {
  32. for (uint_t j = 0; j < Z; j++) {
  33. cs[i][j] = 0;
  34. }
  35. }
  36.  
  37. uint_t mcs[2] = {0, 0};
  38. uint_t pl = 0, pr = 0;
  39. uint_t res = n <= k + 2 ? n : k + 2;
  40. if (n <= k + 2)
  41. goto out;
  42. while (pr < n) {
  43. uint_t prm2 = pr % 2;
  44. uint_t prc = as[pr++];
  45. cs[prm2][prc]++;
  46. uint_t omc = mcs[prm2];
  47. if (prc != omc) {
  48. if (cs[prm2][prc] > cs[prm2][omc]) {
  49. mcs[prm2] = prc;
  50. }
  51. }
  52.  
  53. for (;;) {
  54. uint_t d = pr - pl - cs[0][mcs[0]] - cs[1][mcs[1]];
  55. if (d <= k)
  56. break;
  57. assert (pl < pr);
  58. uint_t plm2 = pl % 2;
  59. uint_t plc = as[pl++];
  60. cs[plm2][plc]--;
  61. if (plc != mcs[plm2])
  62. continue;
  63. uint_t mc = std::max_element(cs[plm2], cs[plm2] + Z) - cs[plm2];
  64. assert (mc < Z);
  65. mcs[plm2] = mc;
  66. }
  67.  
  68. if (res < pr - pl)
  69. res = pr - pl;
  70. }
  71. out:;
  72. printf("%u\n", res);
  73.  
  74.  
  75. return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement