Cod sursa(job #2844101)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 3 februarie 2022 19:18:31
Problema Substr Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.72 kb
#include <fstream>
#include <cstring>
#include <map>

const int MAX_N = 16384;
const int B = 257;
const int P = 1e9 + 7;
int prefH[1 + MAX_N], powerB[1 + MAX_N];
std::map<int, int> mp;
int k;

int rangeHash(int l, int r) {
  return (prefH[r] - (long long)prefH[l - 1] * powerB[r - l + 1] % P + P) % P;
}

bool ok(int n, int l) {
  mp.clear();
  for (int i = 1; i + l - 1 <= n; i++) {
    mp[rangeHash(i, i + l - 1)]++;
  }
  for (auto it : mp) {
    if (it.second >= k) {
      return true;
    }
  }
  return false;
}

int cb(int n) {
  int st = 1, dr = n, sol = 0;
  while (st <= dr) {
    int mijl = (st + dr) / 2;
    if (ok(n, mijl)) {
      sol = mijl;
      st = mijl + 1;
    } else {
      dr = mijl - 1;
    }
  }
  return sol;
}

bool isDigit[256] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0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0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

template<int SIZE>
class ReadStream {
private:
  FILE* stream;

  char buffer[SIZE];
  char* begin;
  char* end;

  void readBuffer() {
    this->begin = this->buffer;
    this->end = this->begin + fread(this->buffer, sizeof(char), SIZE, this->stream);
  }

  char currentChar() {
    return *this->begin;
  }

  void advance() {
    this->begin++;
    if (this->begin == this->end)
      this->readBuffer();
  }

public:
  ReadStream(FILE* stream = stdin) {
    this->stream = stream;
    this->begin = NULL;
    this->end = NULL;
    this->readBuffer();
  }

  char nextChar() {
    char answer = this->currentChar();
    this->advance();
    return answer;
  }

  int nextUnsignedInt() {
    while (isWhiteSpace[(int)this->currentChar()])
      this->advance();
    int answer = 0;
    while (isDigit[(int)this->currentChar()]) {
      answer *= 10;
      answer += this->currentChar() - '0';
      this->advance();
    }
    return answer;
  }

  double nextDouble() {
    double answer = nextUnsignedInt();
    if (this->currentChar() == '.') {
      advance();
      int decimals = 0;
      while (isDigit[(int)this->currentChar()]) {
        answer *= 10;
        answer += this->currentChar() - '0';
        advance();
        decimals++;
      }
      while (decimals > 0) {
        answer /= 10;
        decimals--;
      }
    }
    return answer;
  }

  int nextString(char* answer) {
    while (isWhiteSpace[(int)this->currentChar()])
      this->advance();
    int length = 0;
    while (!isWhiteSpace[(int)this->currentChar()]) {
      answer[length] = this->currentChar();
      length++;
      this->advance();
    }
    answer[length] = 0;
    return length;
  }
};

int main() {
  std::ifstream fin("substr.in");
  std::ofstream fout("substr.out");
  ReadStream<100000> reader;
  int n;
  char* S = new char[1 + MAX_N];
  n = reader.nextUnsignedInt();
  k = reader.nextUnsignedInt();
  reader.nextString(S);
  for (int i = 1; i <= n; i++) {
    prefH[i] = ((long long)prefH[i - 1] * B + S[i - 1] - 'a') % P;
  }
  powerB[0] = 1;
  for (int i = 1; i <= n; i++) {
    powerB[i] = (long long)powerB[i - 1] * B % P;
  }
  int answer = cb(n);
  fout << answer;
  return 0;
}