Cod sursa(job #3266586)

Utilizator Barbu_MateiBarbu Matei Barbu_Matei Data 9 ianuarie 2025 16:15:13
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;

const int p = 11;
string text, word;
bool m[2000001];

int hashF(string& word) {
    int res = 0;
    for (int i = 0; i < word.size(); ++i) {
        res = res * p + (int)word[i] * p;
    }
    return res;
}

int main() {
    ifstream cin("abc2.in");
    ofstream cout("abc2.out");
    cin >> text;
    while (cin >> word) {
        m[hashF(word)] = true;
    }
    int res = 0, pos = 0, ans = 0;
    for (int i = 0; i < text.size(); ++i) {
        ++pos;
        res = res * p + (int)text[i] * p;
        if (pos == word.size()) {
            ans += m[res];
            res = res - (int)text[i - (pos - 1)] * pow(p, pos);
            --pos;
        }
    }
    cout << ans;
}