Cod sursa(job #2790814)

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

using namespace std;

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

const int base = 29;
const int mod = 196613;
const int kN = 1 << 14;
int n, k, 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), s[i] - 'a' + 1);
  }
  ++f[h];
  int m = 0;
  clean[m++] = h;
  for (int i = len; i < n; ++i) {
    sub_self(h, mul(s[i - len] - 'a' + 1, p[len - 1]));
    h = add(mul(h, base), s[i] - 'a' + 1);
    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;
  }
  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;
}