Cod sursa(job #3219043)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 29 martie 2024 20:05:33
Problema Abc2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define ll long long
//#define int long long
using namespace std;

const string TASK("abc2");
ifstream fin(TASK + ".in");
ofstream fout(TASK + ".out");
#define cin fin
#define cout fout

const int N = 2009, P = 3, M = 1e9 + 123;

char s[10000009], t[21];
unordered_map<ll, bool> mp;

signed main()
{
    cin >> s;

    int n = 0;
    for(ll hsh = 0; cin >> t; hsh = 0)
    {
        if(!n)n = strlen(t);
        for(int i = 0; i < n; ++i)
            hsh = hsh * P + t[i] - 'a';
        mp[hsh] = true;
    }

    ll pt = 1, hsh = 0;
    for(int i = 1; i <= n; ++i)pt = pt * P;

    int ans = 0;
    int sz = strlen(s);
    for(int i = 0; i < sz; ++i)
    {
        hsh = hsh * P + s[i] - 'a';
        if(i >= n)hsh = hsh - (s[i - n] - 'a') * pt;

        if(i + 1 < n)continue;

        if(mp.find(hsh) != mp.end())ans ++;
    }

    cout << ans << '\n';
    return 0;
}