Cod sursa(job #1365293)

Utilizator ArchazeyBaltatu Andrei-Mircea Archazey Data 28 februarie 2015 11:07:07
Problema Substr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
#include<bits/stdc++.h>
using namespace std;

struct cell
{
    int x,y,frecv;
};

ifstream fin("substr.in");
ofstream fout("substr.out");

const int baza1=37;
const int baza2=73;
const int MODULO1=100007;
const int MODULO2=1000007;
const int NMAX=20000;

int n,k,how[NMAX],put1[NMAX],put2[NMAX];
char s[NMAX];
vector<cell>H[MODULO1];
vector<cell>::iterator it;

inline int Add(int aux,int paux)
{
    for (it=H[aux].begin();it!=H[aux].end();it++)
        if ((*it).x==aux && (*it).y==paux)
            {
                (*it).frecv++;
                return (*it).frecv;
            }
    cell k;
    k.x=aux;k.y=paux;k.frecv=1;
    H[aux].push_back(k);
    return 1;
}

int main()
{
    int i,st,dr,mij,sol=0,aux,paux;
    bool ok;
    fin>>n>>k;
    fin>>(s+1);
    put1[0]=put2[0]=1;
    for (i=1;i<=n;i++)
        {
            put1[i]=(put1[i-1]*baza1)%MODULO1;
            put2[i]=(put2[i-1]*baza2)%MODULO2;
            if (s[i]>='0' && s[i]<='9') how[i]=s[i]-'0'+1;
            else
            if (s[i]>='a' && s[i]<='z') how[i]=s[i]-'a'+1+10;
            else
            if (s[i]>='A' && s[i]<='Z') how[i]=s[i]-'A'+1+36;
        }
    st=1;dr=NMAX;
    while (st<=dr)
        {
            mij=(st+dr)>>1;
            aux=paux=ok=0;
            for (i=1;i<=n;i++)
                {
                    aux=(aux*baza1+how[i])%MODULO1;
                    paux=(paux*baza2+how[i])%MODULO2;
                    if (i>mij)
                        {
                            aux-=(how[i-mij]*put1[mij])%MODULO1;
                            if (aux<0) aux+=MODULO1;
                            paux-=(how[i-mij]*put2[mij])%MODULO2;
                            if (paux<0) paux+=MODULO2;
                        }
                    if (i>=mij) {if (Add(aux,paux)>=k) ok=1;}
                }
            if (ok==1) {sol=mij;st=mij+1;}
            else dr=mij-1;

            //golire
            for (i=0;i<MODULO1;i++) H[i].clear();
        }
    fout<<sol<<"\n";
    return 0;
}