Cod sursa(job #2871623)

Utilizator darisavuSavu Daria darisavu Data 15 martie 2022 10:37:57
Problema Abc2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.9 kb

#include <fstream>
#include <cstring>
#include <algorithm>

using namespace std;

const int NL = 3;
const int K = 666019;
const int NC = 50001;
const int LT = 1e7 + 1;
const int LC = 21;

char text[LT], cuvant[LC];

unsigned int val[NC], urm[NC], lst[K], nr;

bool exista(unsigned int x)
{
    unsigned int categoria = x % K;
    unsigned int p = lst[categoria];
    while (p != 0)
    {
        if (val[p] == x)
        {
            return true;
        }
        p = urm[p];
    }
    return false;
}

void adauga(unsigned int x)
{
    if (exista(x))
    {
        return;
    }
    unsigned int categoria = x % K;
    val[++nr] = x;
    urm[nr] = lst[categoria];
    lst[categoria] = nr;
}

unsigned int codul(char *s)
{
    unsigned int cod = 0, i = 0;
    while (s[i] != '\0')
    {
        cod = cod * NL + (s[i++] - 'a');
    }
    return cod;
}

int main()
{
    ios::sync_with_stdio(false);
    ifstream in("abc2.in");
    ofstream out("abc2.out");
    in.tie(0);
    in.get(text, LT);
    in.get();
    in.get(cuvant, LC);
    in.get();
    unsigned int l = strlen(cuvant), nr = 0;
    while (!in.eof())
    {
        adauga(codul(cuvant));
        in.get(cuvant, LC);
        in.get();
    }
    unsigned int p3 = 1;
    for (unsigned int i = 0; i < l - 1; i++)
    {
        p3 *= 3;
    }
    unsigned int vp3[3] = {0, p3, 2 * p3};
    unsigned int cod = 0;
    for (unsigned int i = 0;  text[i] != '\0'; i++)
    {
        if (i < l - 1)
        {
            cod = cod * NL + (text[i] - 'a');
        }
        else
        {
            if (i >= l)
            {
                cod -= vp3[text[i-l] - 'a'];
            }
            cod = cod * NL + (text[i] - 'a');
            if (exista(cod))
            {
                nr++;
            }
        }
    }
    out << nr;
    in.close();
    in.close();
    return 0;
}