Cod sursa(job #2834848)

Utilizator rapidu36Victor Manz rapidu36 Data 17 ianuarie 2022 19:25:34
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 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()
{
    ifstream in("abc2.in");
    ofstream out("abc2.out");
    in >> text;
    while (in >> cuvant)
    {
        adauga(codul(cuvant));
    }
    unsigned int l = strlen(cuvant), nr = 0;
    unsigned int p3 = 1;
    for (unsigned int i = 0; i < l - 1; i++)
    {
        p3 *= 3;
    }
    unsigned int cod = 0;
    for (unsigned int i = 0;  text[i] != '\0'; i++)
    {
        if (i < l - 1)
        {
            cod = cod * NL + (text[i] - 'a');
        }
        else
        {
            cod = cod % p3 * NL + (text[i] - 'a');
            if (exista(cod))
            {
                nr++;
            }
        }
    }
    out << nr;
    in.close();
    in.close();
    return 0;
}