Cod sursa(job #2813770)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 7 decembrie 2021 11:09:14
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <set>

using namespace std;

ifstream fin("abc2.in");
ofstream fout("abc2.out");

const int base = 3;
int ans;
string s, cuv;
set <unsigned int> dictionar;

int len, s_len;
unsigned int base_pow, modh;

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
    fin >> s;
    s_len = s.size();
    while(fin >> cuv)
    {
        len = cuv.size();
        unsigned int myHash = 0;
        for(int i = 0; i < len; i++)
            myHash = myHash * base + (cuv[i] - 'a');
        if(dictionar.find(myHash) == dictionar.end())
            dictionar.insert(myHash);
    }
    base_pow = 1;
    for(int i = 1; i < len; i++)
        base_pow *= base;
    unsigned int myHash = 0;
    for(int i = 0; i < len; i++)
        myHash = myHash * base + (s[i] - 'a');
    if(dictionar.find(myHash) != dictionar.end())
        ans++;
    for(int i = len; i < s_len; i++)
    {
        int val = s[i - len] - 'a';
        myHash = myHash - base_pow * val ;
        myHash = myHash * base + (s[i] - 'a');
        if(dictionar.find(myHash) != dictionar.end())
            ans++;
    }
    fout << ans;
    return 0;
}