Cod sursa(job #1525095)

Utilizator felixiPuscasu Felix felixi Data 14 noiembrie 2015 18:58:13
Problema Substr Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <map>
#include <cstring>
#include <string>
#include <deque>

using namespace std;

ifstream in("substr.in");
ofstream out("substr.out");

const int NMAX = 16500;

map < deque<char>, int > MAP;
string s;
int N, K, Ans = 0;

int CONT( int L ) {
    deque< char > DQ;
    int cnt = 0;
    for( int ind = 1;  ind <= N;  ++ind ) {
        DQ.push_back( s[ind] );
        if( ind - L >= 1 ) {
            DQ.pop_front();
        }
        if( ind - L >= 0 ) {
            MAP[ DQ ]++;
            cnt = max( cnt, MAP[ DQ ] );
        }
    }
    return cnt;
}

int main() {
    in >> N >> K;
    in >> s;
    s = "#" + s;

    int step = 1;
    for( ;  step * 2 <= N;  step <<= 1 );
    for( Ans = 0;  step;  step >>= 1 ) {
        if( CONT(Ans+step) >= K )  Ans += step;
    }
    out << Ans << '\n';
    return 0;
}