Pagini recente » Clasament cel_mai_mare_olimpicar_2019_oni2008_zi2 | Clasament nu_poate_veni_deci_nu_e_shimulare | Autentificare | Istoria paginii runda/simulare-cartita-19a | Cod sursa (job #1083485)
#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,i,p;
fscanf(f,"%d %d\n",&n,&k);
fgets (a , nmax , f);
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;
}