Cod sursa(job #2831102)

Utilizator rapidu36Victor Manz rapidu36 Data 10 ianuarie 2022 20:42:12
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <map>
#include <string>
#include <iostream>

using namespace std;

const int NL = 3;

map <unsigned int, bool> c;

unsigned int codul(string &s)
{
    unsigned int cod = 0;
    for (auto c: s)
    {
        cod = cod * NL + (c - 'a');
    }
    return cod;
}

int main()
{
    ifstream in("abc2.in");
    ofstream out("abc2.out");
    string text, cuvant;
    in >> text;
    while (in >> cuvant)
    {
        c[codul(cuvant)] = true;
    }
    int l = cuvant.length(), nr = 0;
    unsigned int p3 = 1;
    for (int i = 0; i < l - 1; i++)
    {
        p3 *= 3;
    }
    unsigned int cod = 0;
    for (int i = 0;  i < text.length(); i++)
    {
        if (i < l - 1)
        {
            cod = cod * NL + (text[i] - 'a');
        }
        else
        {
            cod = cod % p3 * NL + (text[i] - 'a');
            if (c[cod])
            {
                nr++;
            }
        }
    }
    out << nr;
    in.close();
    in.close();
    return 0;
}