Pagini recente » Cod sursa (job #55838) | Cod sursa (job #1435623) | Cod sursa (job #2819060) | Cod sursa (job #2063970) | Cod sursa (job #1392638)
#include <iostream>
#include <fstream>
#include <tr1/unordered_map>
using namespace std;
ifstream in ("substr.in");
ofstream out ("substr.out");
const int MAXN = 16384 + 1;
const int power = 71;
typedef unsigned long long ULL;
int N, K;
char S[MAXN];
ULL P[MAXN];
tr1 :: unordered_map <ULL, int> M;
void Preprocess ()
{
int i;
P[1] = power;
for (i = 2; i <= N; i ++)
P[i] = P[i - 1] * power;
}
bool ok (int len)
{
int i;
int NrMax = 1;
ULL hash = 0;
for (i = 0; i < len; i ++)
hash = hash * power + S[i];
M[hash] ++;
for (i = len; i < N; i ++){
hash = hash - S[i - len] * P[len - 1];
hash = hash * power + S[i];
M[hash] ++;
if (M[hash] > NrMax)
NrMax = M[hash];
}
return (NrMax >= K);
}
int BSearch ()
{
int i, step;
for (step = 1; step < N; step <<= 1 );
for (i = 0; step; step >>= 1)
if (i + step <= N && ok (i + step))
i += step;
return i;
}
int main()
{
in >> N >> K;
in >> S;
Preprocess ();
out << BSearch ();
return 0;
}