Cod sursa(job #3209779)

Utilizator PiciuAndreiAlinPiciu Andrei Alin PiciuAndreiAlin Data 3 martie 2024 15:17:43
Problema Substr Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>
#define P 123457
#define Q 777013
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
/**
'A' - 'Z' = 0 - 25
'a' - 'z' = 26 - 51
'0' - '9' = 52 - 62
*/
int Cifra(char ch)
{
    if('A' <= ch and ch <= 'Z')return ch - 'A';
    if('a' <= ch and ch <= 'z')return 26 + (ch - 'a');
    return 52 + (ch - '0');
}
string a;
int n, m, k;
int Cauta(int q)
{
    unordered_map<int, int>M;
    int p, p1, x1, x2, x3, x4, i;
    p = p1 = 1;
    x1 = x2 = x3 = x4 = 0;
    for(i = 1; i < q; i++)
    {
        p = (p * 62) % P;
    }
    for(i = 0; i < q; i++)
    {
        x1 = (x1 * 62 + (Cifra(a[i]))) % P;
    }
    M[x1]++;
    for(i = q; i < n; i++)
    {
        x1 = (x1 - Cifra(a[i - q]) * p % P + P) % P;
        x1 = (x1 * 62 + Cifra(a[i])) % P;
        M[x1]++;
    }
    for(auto i : M)
        if(i.second >= k)return 1;
    return 0;
}
int main()
{
    int st, dr, poz, mij;
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    fin >> n >> k;
    fin >> a;
    st = 1;
    dr = n;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(Cauta(mij) == 1)
        {
            poz = mij;
            st = mij + 1;
        }
        else dr = mij - 1;
    }
    fout << poz;
    return 0;
}