Cod sursa(job #3209589)

Utilizator mateilucaLuca Matei Gabriel mateiluca Data 2 martie 2024 21:53:21
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>
#define P 777013
#define Q 203459

using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
int cnt, L, n;
char text[10000005], cuv[25];
unordered_map<long long, bool> M;

int main()
{
    int i, x1, x2, p1, p2;
    fin >> text;
    while(fin >> cuv)
    {
        x1 = x2 = 0;
        for(i = 0;cuv[i] != 0;i++)
        {
            x1 = (x1 * 3 + cuv[i] - 'a') % P;
            x2 = (x2 * 3 + cuv[i] - 'a') % Q;
        }
        M[1LL * x1 * 777013 + x2] = 1;
    }
    L = strlen(cuv);
    x1 = x2 = 0;
    p1 = p2 = 1;
    for(i = 1;i < L;i++)
    {
        p1 = p1 * 3 % P;
        p2 = p2 * 3 % Q;
    }
    for(i = 0;i < L;i++)
    {
        x1 = (x1 * 3 + text[i] - 'a') % P;
        x2 = (x2 * 3 + text[i] - 'a') % Q;
    }
    if(M[1LL * x1 * 777013 + x2] == 1)
        cnt++;
    for(i = L;text[i] != 0;i++)
    {
        x1 = (x1 - p1 * (text[i - L] - 'a') + P) % P;
        x1 = (x1 * 3 + text[i] - 'a') % P;
        x2 = (x2 - p2 * (text[i - L] - 'a') + Q) % Q;
        x2 = (x2 * 3 + text[i] - 'a') % Q;
        if(M[1LL * x1 * 777013 + x2] == 1)
            cnt++;
    }
    fout << cnt << "\n";
    fout.close();
    return 0;
}