Cod sursa(job #2238674)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 6 septembrie 2018 22:18:24
Problema Substr Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <cstring>
#include <unordered_map>
 
using namespace std;
 
ifstream fin ("substr.in");
ofstream fout ("substr.out");
 
const int mod1 = 1013, mod2 = 1013,base1 = 19,Dim = 170385,base2 = 29;
const int NMAX = (1 << 14);
const int MOD = 1000000007;
const int P = 26;
int Hash[mod1][mod2],n,k,Powbase1[Dim],Powbase2[Dim];

char s[1 + NMAX];
int hash1[1 + NMAX];
int put[1 + NMAX];
char T[Dim];
bool Check(int lung);
int main() {
     
    fin >> n >> k >> ( T + 1 );
    put[0] = 1;
	for(int i = 1; i <= n; i++) {
    put[i] = 1LL * put[i - 1] * P % MOD;
    hash1[i] = (1LL * hash1[i - 1] * P + T[i] - 'a') % MOD;
  }

    int st = 1, dr = n / k + 1,mj,sol = 0;
    while ( st <= dr) {
        mj = ( st + dr ) / 2;
        if ( Check(mj) ) 
            st = mj + 1, sol = mj;
        else
            dr = mj - 1;
    }
    fout << sol;
}
 


bool Check(int lg) {
  int sol = 1;
  unordered_map <int, int> f;
  f[hash1[lg]]++;
  for(int i = lg + 1; i <= n; i++) {
    int h = (hash1[i] - (1LL * hash1[i - lg] * put[lg]) % MOD + MOD) % MOD;
    f[h]++;
  }
  for(auto it : f)
    sol = max(sol, it.second);
  return (sol >= k);
}