Pagini recente » Cod sursa (job #670203) | Cod sursa (job #529155) | Borderou de evaluare (job #2269968) | Cod sursa (job #57491) | Cod sursa (job #2380611)
#include <cstdio>
#include <vector>
#define NMAX 16385
#define MOD 733331
#define PRIM 33
using namespace std;
typedef unsigned int ui;
char S[NMAX];
ui n, k;
vector <ui> hashTable;
ui power(ui x, ui n)
{
if(n == 0)
return 1;
if(n == 1)
return x;
ui tmpPow = power(x, n >> 1);
if(n & 1)
return tmpPow * tmpPow * x;
return tmpPow * tmpPow;
}
bool okHash(ui q)
{
hashTable = vector <ui> (MOD, 0);
ui i, P = power(PRIM, q - 1), hashQ = 0, tmpCalc;
for(i=0; i<q; ++i)
hashQ = ((hashQ << 5) + hashQ) + (S[i] - '`');
++hashTable[hashQ % MOD];
for(i=q; i<n; ++i){
tmpCalc = hashQ - ((S[i - q] - '`') * P);
hashQ = ((tmpCalc << 5) + tmpCalc) + (S[i] - '`');
//printf("%u\n", hashQ);
++hashTable[hashQ % MOD];
if(hashTable[hashQ % MOD] >= k)
return true;
}
return false;
}
ui cautaBinDim(ui st, ui dr)
{
ui dim = 0;
while(st <= dr){
ui mij = (st + dr) >> 1;
if(okHash(mij)){
dim = mij;
st = mij + 1;
}
else dr = mij - 1;
}
return dim;
}
int main()
{
ui i, dim = 0;
freopen("substr.in","r",stdin);
freopen("substr.out","w",stdout);
scanf("%u %u\n%s", &n, &k, S);
for(i=n - 1; i>1; --i)
if(okHash(i)){
printf("%u\n", i);
return 0;
}
//printf("%u\n", dim);
//printf("%u\n", cautaBinDim(0, n - 1));
return 0;
}