Cod sursa(job #2200180)

Utilizator MaligMamaliga cu smantana Malig Data 30 aprilie 2018 16:18:08
Problema Substr Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 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;
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 hash1 = 0, put1 = 1;
        int hash2 = 0, put2 = 1;
        const int mod1 = 10000007, mod2 = 10000021, base = 73;
        for (int i = 0; i < len; ++i) {
            hash1 = (hash1 * base + (str[i]-'a') * base) % mod1;
            hash2 = (hash2 * base + (str[i]-'a') * base) % mod2;
            put1 = (put1 * base) % mod1;
            put2 = (put2 * base) % mod2;
        }

        int mx = 0;
        map<pair<int,int>, int> occ;
        for (int i = len; i < N; ++i) {
            hash1 = ((hash1 - ((str[i - len] - 'a') * put1 % mod1) + mod1) * base + (str[i] - 'a') * base) % mod1;
            hash2 = ((hash2 - ((str[i - len] - 'a') * put2 % mod2) + mod2) * base + (str[i] - 'a') * base) % mod2;
            pair<int,int> p = {hash1, hash2};
            int& r = occ[p];
            r += 1;

            if (mx < r) {
                mx = r;
            }
        }

        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;
}