Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include <inttypes.h>
- #include <string.h>
- #include <algorithm>
- typedef unsigned int uint_t;
- const uint_t N = 5000000, Z = 26, ZA = 32;
- static uint8_t as[N + 1];
- int main()
- {
- uint_t n = 0, k = 0;
- int st = scanf("%u%u", &n, &k);
- assert (st == 2);
- assert (0 < n && n <= N);
- assert (k <= N);
- st = scanf("%5000000s", as);
- assert (st == 1);
- as[N] = 0;
- assert (strlen((const char *) as) == n);
- // fprintf(stderr, "n=%u, k=%u, as='%s' \n", n, k, as);
- for (uint_t i = 0; i < n; i++) {
- uint_t x = as[i] - 'a';
- assert (x < Z);
- as[i] = x;
- }
- uint_t cs[2][ZA];
- for (uint_t i = 0; i < 2; i++) {
- for (uint_t j = 0; j < Z; j++) {
- cs[i][j] = 0;
- }
- }
- uint_t mcs[2] = {0, 0};
- uint_t pl = 0, pr = 0;
- uint_t res = n <= k + 2 ? n : k + 2;
- if (n <= k + 2)
- goto out;
- while (pr < n) {
- uint_t prm2 = pr % 2;
- uint_t prc = as[pr++];
- cs[prm2][prc]++;
- uint_t omc = mcs[prm2];
- if (prc != omc) {
- if (cs[prm2][prc] > cs[prm2][omc]) {
- mcs[prm2] = prc;
- }
- }
- for (;;) {
- uint_t d = pr - pl - cs[0][mcs[0]] - cs[1][mcs[1]];
- if (d <= k)
- break;
- assert (pl < pr);
- uint_t plm2 = pl % 2;
- uint_t plc = as[pl++];
- cs[plm2][plc]--;
- if (plc != mcs[plm2])
- continue;
- uint_t mc = std::max_element(cs[plm2], cs[plm2] + Z) - cs[plm2];
- assert (mc < Z);
- mcs[plm2] = mc;
- }
- if (res < pr - pl)
- res = pr - pl;
- }
- out:;
- printf("%u\n", res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement