Cod sursa(job #1069076)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 29 decembrie 2013 13:30:37
Problema Substr Scor 20
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.13 kb
#include <fstream>
#include <map>
using namespace std;

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

const int NMAX = 16384;
const int BASE = 123;

string data;

map<unsigned int, int> hashMap;

int N, K;

bool isMatching(int value){

    unsigned int hashKey = 0, basePower = 1, i, v, result = 0; //dispersion by overflow

    for(i = 0; i < value; i++){
        hashKey = hashKey*BASE + data[i];
        basePower *= BASE;
    }

    basePower /= BASE;

    hashMap[hashKey] = 1;

    for(i = value; i < N; i++){
        hashKey = (hashKey - data[i-value] * basePower) * BASE + data[i];

        v = ++hashMap[hashKey];
        if(v > result)
            result = v;
    }

    hashMap.clear();

    if(result >= K)
        return true;
    else return false;
}

int main()
{
    in >> N >> K >> data;

    int step = 1, result ;

    while(step <= N) step<<=1;

    /*for(result = 0; step; step>>=1){
        hashMap.clear();
        if(result + step <= N && isMatching(result+step))
            result+=step;
    }*/
    for(result = 1; result <= N  && isMatching(result); result++)
        ;
    out << result -1 << '\n';
    return 0;
}