Cod sursa(job #3209796)

Utilizator PiciuAndreiAlinPiciu Andrei Alin PiciuAndreiAlin Data 3 martie 2024 15:43:53
Problema Substr Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 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;
    vector<pair<int, int>>sol;
    int Cauta(int q)
    {
        int p, p1, x1, x2, x3, x4, i, l = 1, lgmax = 0;
        p = p1 = 1;
        x1 = x2 = x3 = x4 = 0;
        for(i = 1; i < q; i++)
        {
            p = (p * 62) % P;
            p1 = (p1 * 62) % Q;
        }
        for(i = 0; i < q; i++)
        {
            x1 = (x1 * 62 + (Cifra(a[i]))) % P;
            x2 = (x2 * 62 + (Cifra(a[i]))) % Q;
        }
        sol.push_back({x1, x2});
        for(i = q; i < n; i++)
        {
            x1 = (x1 - Cifra(a[i - q]) * p % P + P) % P;
            x1 = (x1 * 62 + Cifra(a[i])) % P;
            x2 = (x2 - Cifra(a[i - q]) * p1 % Q + Q) % Q;
            x2 = (x2 * 62 + Cifra(a[i])) % Q;
            sol.push_back({x1, x2});
        }
        sort(sol.begin(), sol.end());
        for(i = 1; i < sol.size(); i++)
            if(sol[i] == sol[i - 1])
            {
                l++;
                lgmax = max(lgmax, l);
            }
            else l = 1;
        if(lgmax >= 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;
    }