Cod sursa(job #1072933)

Utilizator Aleks10FMI - Petrache Alex Aleks10 Data 5 ianuarie 2014 13:13:02
Problema Substr Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

unordered_map<int,int> unmap;
//char a[17000];
string a;
int n,k,j;
string substr;
const int BASE = 123;

int verifica(int nrap){
   /* int i=0;
    int imp=nrap;
    int maxim=-1;
        for(i=0;i<n-imp;i++){
            substr.clear();
            for(j=i;j<i+imp+1;j++){
                substr+=a[j];
            }
            unmap[substr]++;
            if(unmap[substr]>maxim)
                maxim=unmap[substr];
            //cout<<imp<<" cu "<<unmap[substr]<<" "<<substr;
           // cout<<'\n';
        }
        //cout<<maxim;
 //       if(maxim>=k){
            return (maxim);
   //     }
     //   imp--;*/

    int key = 0, power = 1;
    for ( int i = 0; i < nrap; ++i )
        key = key * BASE + (int)a[i];
    for ( int i = 1; i < nrap; ++i )
        power=power*BASE;

    unmap[key]=1;

    int rasp=-1;
    for(int i=nrap;i<n;i++){
        key -= a[i - nrap] * power;
        key = key * BASE + (int) a[i];

        unmap[key]++;
        if(rasp<unmap[key])
            rasp=unmap[key];
    }
    unmap.clear();
    return rasp;
}

int main()
{
    ifstream f("substr.in");
    ofstream g("substr.out");
    f>>n>>k;
    int i;
    //for(i=0;i<n;i++)
        //f>>a[i];
    f>>a;

    /*substr+='c';
    substr+='d';
    substr.clear();
    substr+='c';*/
    //cout<<substr;
    int p=1<<15;
  //  cout<<p;
    //cout<<verifica(1);
    for(i=0;p!=0;p=p/2)
        if(i+p<n && verifica(i+p)>=k){
            //cout<<i<<" si "<<p<<'\n';
            i+=p;
        }
    g<<i;
    return 0;
}