Cod sursa(job #1520459)

Utilizator lflorin29Florin Laiu lflorin29 Data 8 noiembrie 2015 19:59:04
Problema Substr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

const unsigned base = 1000000007, mmr = 16385;

vector<int>Pow(mmr), Hash(mmr);
int N, M;
char S[mmr];

void dohash() {
    Pow[0] = 1;
    for(int i = 1; i < mmr; ++i)
        Pow[i] = Pow[i - 1] * base;
    for(int i = 1; i <= N; ++i)
        Hash[i] = Hash[i - 1] * base + S[i];
}

unsigned h(int x, int y) {
    return Hash[y] - Hash[x - 1] * Pow[y - x + 1];
}

bool able(const int &val) {
    unordered_map<unsigned, int> freq;
    for(int i = val; i <= N; ++i) {
       int wh = h(i - val + 1, i);
       ++freq[wh];
       if(freq[wh] >= M)
         return 1;
    }
    return 0;
}

int main() {
    ifstream cin("substr.in");
    ofstream cout("substr.out");
    cin >> N >> M >> S + 1;
    dohash();

    int ans = 0;
    for(int step = 16384; step; step /= 2)
        if(step + ans <= N && able(step + ans))
           ans += step;
    cout << ans;
    return 0;
}