Cod sursa(job #2768646)

Utilizator DragosC1Dragos DragosC1 Data 11 august 2021 17:51:13
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;

string s, aux;
long long base;
int l, l2;

unordered_map<long long, bool> mp;

void read() {
    int i, j;
    long long hash;
    ifstream f("abc2.in");
    f >> s;
    while (f >> aux) {
        hash = 0;
        base = 1;
        for (j = 0; j < aux.size(); j++) 
            hash = hash * 3 + (aux[j] - 'a');
        l2 = aux.size();
        mp[hash] = 1;
    }
}

int nr;

void solve() {
    int i;

    long long hash = 0;
    for (i = 0; i < l2; i++) {
        hash = hash * 3 + (s[i] - 'a');
        base = base * 3;
    }
    base /= 3;
    if (mp.find(hash) != mp.end())
        nr++;

    for (i = l2; i < s.size(); i++) {
        hash = (hash - (s[i - l2] - 'a') * base) * 3 + (s[i] - 'a');
        if (mp.find(hash) != mp.end())
            nr++;
    }
}

void output() {
    ofstream g("abc2.out");
    g << nr;
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}