Cod sursa(job #3209926)

Utilizator robert_dumitruDumitru Robert Ionut robert_dumitru Data 3 martie 2024 21:17:37
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m;
string a, b;
unordered_map<long long, bool> fr;

int main()
{
    ios_base :: sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);

    int i, cnt;
    long long p, x;

    fin >> a >> b;
    n = a.length(); m = b.length();

    p = 1;
    for (i = 1; i < m; i++)
        p *= 3;
    x = 0;
    for (i = 0; i < m; i++)
        x = (x * 3 + (a[i] - 'a'));

    fr[x] = 1;
    for (i = m; i < n; i++)
    {
        x = (x - p * (a[i - m] - 'a'));
        x = (x * 3 + (a[i] - 'a'));
        fr[x] = 1;
    }

    cnt = 0;
    do
    {
        x = 0;
        for (i = 0; i < m; i++)
            x = (x * 3 + (b[i] - 'a'));
        cnt += fr[x];
        fr[x] = 0;

    }while (fin >> b);

    fout << cnt << "\n";
    return 0;
}