Cod sursa(job #2200159)

Utilizator MaligMamaliga cu smantana Malig Data 30 aprilie 2018 15:21:44
Problema Substr Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>

using namespace std;

#if 1
    #define pv(x) cout<<#x<<" = "<<(x)<<"; ";cout.flush()
    #define pn cout<<endl
#else
    #define pv(x)
    #define pn
#endif

#ifdef INFOARENA
    ifstream in("substr.in");
    ofstream out("substr.out");
#else 
    #define in cin
    #define out cout
#endif

using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
#define pb push_back
#define mp make_pair
const int NMax = 2e4 + 5;
const ll inf_ll = 1e18 + 5;
const int inf_int = 1e9 + 5;
const int mod = 100003;
using zint = int;

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);

    int N,K;
    in >> N >> K;
    string str;
    in >> str;
    
    int pw = 1;
    for (; pw <= N; pw <<= 1) ;
    pw >>= 1;

    auto isGood = [&str, N, K] (int len) {
        
        int hash = 0, put = 1;
        const int mod = 1e5 + 3, base = 73;
        for (int i = 0; i < len; ++i) {
            hash = (hash * base + (str[i]-'a') * base) % mod;
            put = (put * base) % mod;
        }

        int mx = 0;
        unordered_map<int, int> occ;
        for (int i = len; i < N; ++i) {
            hash = ((hash - ((str[i - len] - 'a') * put % mod) + mod) * base + (str[i] - 'a') * base) % mod;
            occ[hash] += 1;

            if (mx < occ[hash]) {
                mx = occ[hash];
            }
        }

        return mx >= K ? true : false;
    };

    int ans = 0;
    while (pw) {
        if (ans + pw <= N && isGood(ans + pw)) {
            ans += pw;
        }

        pw >>= 1;
    }

    out << ans;

    return 0;
}