Cod sursa(job #1048213)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 5 decembrie 2013 16:26:06
Problema Substr Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <tr1/unordered_map>

using namespace std;
using namespace tr1;

const char infile[] = "substr.in";
const char outfile[] = "substr.out";

ifstream cin(infile);
ofstream cout(outfile);

const int MAXN = 16390;
const int BASE = 33;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, K;
string S;
unordered_map <string, int> Hash;

inline bool Check(int x) {
    if( K == 1 )
        return true;
    Hash.clear();
    Hash[S.substr(0, x)] = 1;
    for(int i = x ; i + x <= N ; ++ i)
        if(++ Hash[S.substr(i, x)] >= K)
            return 1;
    return 0;
}

int main() {
    cin >> N >> K >> S;
    int st = 0, dr = N;
    /// (st + dr)/2 = st / 2 + dr / 2 = st / 2 - dr / 2 + dr = dr + (st - dr) / 2
    int Ans = 0;
    while(st <= dr) {
        int mid = ((st + dr) >> 1);
        if(Check(mid)) {
            Ans = mid;
            st = mid + 1;
        }
        else dr = mid - 1;
    }
    cout << Ans << '\n';
    cin.close();
    cout.close();
    return 0;
}