Pagini recente » Cod sursa (job #2229578) | Cod sursa (job #902902) | Cod sursa (job #1083481)
#include <cstdio>
#include <unordered_map>
#define nmax 16384
FILE *f,*g;
using namespace std;
unordered_map <int,int> M;
char a[nmax];
const int BASE = 123;
int verifica(int x,int n)
{
int key = 0, power = 1;
for(int i=0 ; i<x ; i++)
{
key = key * BASE + (int)a[i];
}
for(int i=1 ; i<x ; i++)
{
power = power * BASE;
}
M[key] = 1;
int rasp = 0;
for(int i=x ; i<n ; i++)
{
key -= a[i - x] * power;
key = key * BASE + (int)a[i];
M[key]++;
if(rasp < M[key])
rasp = M[key];
}
M.clear();
return rasp;
}
int main()
{
f=fopen("substr.in","r");
g=fopen("substr.out","w");
int n,k,j,i,p;
fscanf(f,"%d %d",&n,&k);
fscanf(f,"%s",&a);
p = 1<<15;
if(k==1)
i=n;
else
{
for(i=0;p!=0;p=p/2)
{
if(i+p<=n && verifica(i+p,n)>=k)
{
i+=p;
}
}
}
fprintf(g,"%d",i);
fclose(f);
fclose(g);
return 0;
}