Cod sursa(job #2191431)

Utilizator felixiPuscasu Felix felixi Data 2 aprilie 2018 20:17:37
Problema Substr Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 16400;
const int base = 10 + 26 + 26 + 1;
const int MOD  = 666013;

unordered_map<char, int> lib;
char str[NMAX+2];
int N, K;

int CNT(int lg)
{
    unordered_map<int, int> fr;
    int h = 0, pp = 1;
    for( int i = 1;  i <= lg;  ++i, pp = 1LL * pp * base % MOD  )
        h = (1LL * h * base + lib[ str[i] ]) % MOD;
    fr[h] = 1;
    bool ok = 0;
    for( int i = lg + 1;  i <= N;  ++i ) {
        h = (1LL * h * base) % MOD;
        h = (1LL * h - ((1LL * pp * lib[ str[i - lg] ] ) % MOD) + 2 * MOD) % MOD;
        h = (1LL * h + lib[ str[i] ]) % MOD;
        ++fr[h];
        if( fr[h] >= K )
            ok = 1;
    }
    return ok;
}

int main()
{
    for( char ch = 'a';  ch <= 'z';  ++ch )
        lib[ch] = ch - 'a' + 1;
    for( char ch = 'A';  ch <= 'Z';  ++ch )
        lib[ch] = ch - 'A' + 1 + 26;
    for( char ch = '0';  ch <= '9';  ++ch )
        lib[ch] = ch - '0' + 1 + 26 + 26;
    in >> N >> K;
    in >> (str + 1);
    int step = (1 << 20);
    int ans = 0;
    while(step) {
        if( ans + step <= N && CNT(ans + step) )
            ans += step;
        step >>= 1;
    }
    out << ans << '\n';
    return 0;
}