Cod sursa(job #1083484)

Utilizator tavi.belu1994FMI Belu Andrei Octavian tavi.belu1994 Data 16 ianuarie 2014 00:15:36
Problema Substr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#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;
}