Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 5 * 1e6 + 11;
- struct myset {
- vector<int> cnt;
- int mx = 0;
- myset() {
- cnt.resize(maxn);
- }
- int get_max() {
- return mx;
- }
- void decrease(int x) {
- if (x == mx && cnt[x] == 1)
- mx -= 1;
- if (x >= 0)
- cnt[x]--;
- if (x > 0)
- cnt[x - 1]++;
- }
- void increase(int x) {
- if (x == mx)
- mx += 1;
- if (x >= 0)
- cnt[x]--;
- cnt[x + 1]++;
- }
- };
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- #ifdef LOCAL
- freopen("input", "r", stdin);
- #endif
- int n, k;
- cin >> n >> k;
- string s;
- cin >> s;
- int ans = 0;
- int r = 0;
- int cnt_ch['z' + 1], cnt_nch['z' + 1];
- memset(cnt_ch, 0, sizeof cnt_ch), memset(cnt_nch, 0, sizeof cnt_nch);
- myset ch, nch;
- ch.mx = 1;
- ch.cnt[1] = 1;
- cnt_ch[s[0]] += 1;
- auto calc = [&](int l, int r) {
- return (r - l + 1) - ch.get_max() - nch.get_max();
- };
- for (int l = 0; l < n; l++) {
- while (calc(l, r) <= k) {
- r += 1;
- if (r == n)
- break;
- if (r % 2) {
- nch.increase(cnt_nch[s[r]]++);
- } else {
- ch.increase(cnt_ch[s[r]]++);
- }
- }
- if (r % 2) {
- nch.decrease(cnt_nch[s[r]]--);
- } else {
- ch.decrease(cnt_ch[s[r]]--);
- }
- r -= 1;
- ans = max(ans, r - l + 1);
- if (l % 2) {
- nch.decrease(cnt_nch[s[l]]--);
- } else {
- ch.decrease(cnt_ch[s[l]]--);
- }
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement