Cod sursa(job #2956850)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 20 decembrie 2022 20:47:48
Problema Substr Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

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

const int MOD1=1e9;
const int MOD2=1e9+7;

const int BAZA1=31;
const int BAZA2=71;

const int NMAX=16385;

long long pow1[NMAX];
long long pow2[NMAX];
long long hash1[NMAX];
long long hash2[NMAX];

map<long long,int>mp;
string s;

int value(char x)
{
    int aux;
    if(x>='a' && x<='z')
        aux=x-'a';
    else if(x>='A' && x<='Z')
        aux=x-'A'+26;
    else
        aux=x-'0'-10;
    return aux+10;
}

bool verif(int lung,int n,int k)
{
    int i;
    for(i=1;i+lung-1<=n;i++)
    {
        long long aux=(hash1[i+lung-1]-hash1[lung-1]*pow1[(i+lung-1)-lung+1]%MOD1+MOD1)%MOD1;
        mp[aux]++;
    }
    for(auto i:mp)
    {
        if(i.second>=k)
            return true;
    }
    return false;
}

int main()
{
    int n,k,i,j,st=1,dr,mij,kon=0;
    fin>>n>>k;
    fin>>s;
    s="$"+s;
    for(i=1;i<=n;i++)
    {
        pow1[i]=pow1[i-1]*BAZA1;
        //pow2[i]=pow2[i-1]*BAZA2;
        hash1[i]=hash1[i-1]*BAZA1+value(s[i]);
        //hash2[i]=hash2[i-1]*BAZA2+value(s[i]);
        /*if(hash1[i]!=hash2[i])
        {
            hash1[i]=hash1[i]^hash2[i];
            hash2[i]=hash1[i];
        }*/
    }
    st=1;
    dr=n;
    while(st<=dr)
    {
        mij=st+dr;
        mij/=2;
        if(verif(mij,n,k))
        {
            kon=mij;
            st=mij+1;
        }
        else
            dr=mij-1;
    }
    fout<<kon;
    return 0;
}