Cod sursa(job #2200197)

Utilizator MaligMamaliga cu smantana Malig Data 30 aprilie 2018 16:40:27
Problema Substr Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 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) {
         
        ull hash = 0, put = 1;
        const ull base = 77;
        for (int i = 0; i < len; ++i) {
            hash = hash * base + (str[i]-'0') * base;
            put = put * base;
        }
 
        int mx = 0;
        unordered_map<ull, int> occ;
        for (int i = len; i < N; ++i) {
            hash = (hash - ((str[i - len] - '0') * put)) * base + (str[i] - '0') * base;
            int& r = occ[hash];
            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;
}