Cod sursa(job #2790821)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 29 octombrie 2021 16:40:40
Problema Substr Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("substr.in");
ofstream fout("substr.out");

const int kSigma = 1 << 6;
const int kN = 1 << 14 | 1;
const int base = 67;
const int mod = 7444243;
int n, k, val[kSigma], a[kN], p[kN], f[mod], clean[kN];
string s;

void add_self(int &x, const int &y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int add(int x, const int &y) {
  add_self(x, y);
  return x;
}

void sub_self(int &x, const int &y) {
  x -= y;
  if (x < 0) {
    x += mod;
  }
}

int mul(const int &x, const int &y) {
  return (int64_t)x * y % mod;
}

bool check(int len) {
  int h = 0;
  for (int i = 0; i < len; ++i) {
    h = add(mul(h, base), a[i]);
  }
  ++f[h];
  int m = 0;
  clean[m++] = h;
  for (int i = len; i < n; ++i) {
    sub_self(h, mul(a[i - len], p[len - 1]));
    h = add(mul(h, base), a[i]);
    if (++f[h] == k) {
      for (int j = 0; j < m; ++j) {
        f[clean[j]] = 0;
      }
      return true;
    }
    if (f[h] == 1) {
      clean[m++] = h;
    }
  }
  for (int j = 0; j < m; ++j) {
    f[clean[j]] = 0;
  }
  return false;
}

void TestCase() {
  fin >> n >> k >> s;
  if (k == 1) {
    fout << n << '\n';
    return;
  }
  string t = s;
  sort(t.begin(), t.end());
  int cnt = 0;
  val[t[0] - '0'] = ++cnt;
  for (int i = 1; i < n; ++i) {
    if (t[i] != t[i - 1]) {
      val[t[i] - '0'] = ++cnt;
    }
  }
  for (int i = 0; i < n; ++i) {
    a[i] = val[s[i] - '0'];
  }
  p[0] = 1;
  for (int i = 1; i < n - k + 1; ++i) {
    p[i] = mul(p[i - 1], base);
  }
  int l = 1, r = n - k + 1, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  fout << ans << '\n';
}

int main() {
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    TestCase();
  }
  fin.close();
  fout.close();
  return 0;
}