Cod sursa(job #2547032)

Utilizator stefantagaTaga Stefan stefantaga Data 14 februarie 2020 20:57:53
Problema Substr Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
#define MOD 1000000007
#define baza 101
#define MOD2 100000009
#define baza2 103
using namespace std;
ifstream f("substr.in");
ofstream g("substr.out");
int v[16385],k,n,putere[16385];
char ch;
bool ok (int lung)
{
    map <int,int> m;
    long long nr=0,i;
    for (i=1;i<=lung;i++)
    {
        nr=(nr*baza+v[i])%MOD;
    }
    m[nr]++;
    if (m[nr]>=k)
    {
        return 1;
    }
    for (i=lung+1;i<=n;i++)
    {
        nr=(nr-putere[lung-1]*v[i-lung]+MOD)%MOD;
        nr=(nr*baza+v[i])%MOD;
        m[nr]++;
    if (m[nr]>=k)
    {
        return 1;
    }
    }
    return 0;
}
int i,st,dr,mij,sol;
int main()
{
    f>>n>>k;
    putere[0]=1;
    for (i=1;i<=n;i++)
    {
        f>>ch;
        v[i]=int(ch)-48;
        putere[i]=putere[i-1]*baza;
    }
    st=1;
    dr=n;
    while (st<=dr)
    {
        mij=(st+dr)/2;
        if (ok(mij)==1)
        {
            sol=mij;
            st=mij+1;
        }
        else
        {
            dr=mij-1;
        }
    }
    g<<sol;
    return 0;
}