Cod sursa(job #2237944)

Utilizator BeginngerThe Mighty Ginger Beginnger Data 3 septembrie 2018 23:39:57
Problema Substr Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fi("substr.in");
ofstream fo("substr.out");
int n, k;
char c[18002];
map<int, int>mp;
int hassh[18002];
int modpow(int b, int ex)
{
    int sol = 1;
    while(ex)
    {
        if(ex & 1)
            sol = (1LL * sol * b) % 1000000009;
        b = (1LL * b * b) % 1000000009;
        ex >>= 1;
    }
    return sol;
}
int pw[18002];
bool check(int len)
{
    hassh[0] = 1;
    int cc = modpow(30187, len);
    mp.clear();
    for(int i = 1; i <= n; ++i)
    {
        hassh[i] =(1LL * hassh[i-1] * 30187LL + (c[i] - '0') ) % 1000000009;
        int qq = hassh[i];
        if(i >= len)
        {
            qq = (1LL * qq - 1LL * hassh[i - len] * cc + 1LL * 1000000009 * cc) % 1000000009;
            mp[qq]++;
            if(mp[qq]>=k)
                return 1;
        }
    }
    return 0;
}
int main()
{
    fi >> n >> k;
    fi >> (c+1);
    int b = 1;
    int e = n;
    int sol = 0;
    while(b <= e)
    {
        int mid = (b+e) / 2;
        if(check(mid))
            sol = mid, b = mid+1;
        else
            e = mid-1;
    }
    fo << sol;
    return 0;
}