Cod sursa(job #2872385)

Utilizator tomaionutIDorando tomaionut Data 16 martie 2022 21:48:57
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
#define P 10007
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
char s[10000005];
char cuv[30];
vector <int> a[P];
void Add(int x)
{
    int r = x % P;
    a[r].push_back(x);
}

bool Cauta(int x)
{
    int r = x % P;
    for (auto w : a[r])
        if (x == w)
            return 1;
    return 0;
}

int main()
{   
    int cod, h1, i, pow, len, sol = 0;
    fin >> s;
    while (fin >> cuv)
    {
        h1 = 0;
        pow = 1;
        len = strlen(cuv);
        for (i = 0; cuv[i]; i++)
            h1 = (h1 * 97 + cuv[i]) % P;
        
        if (Cauta(h1) == 0)
            Add(h1);
    }
    pow = 1;
    h1 = 0;
    for (i = 0; i < len; i++)
    {
        h1 = (h1 * 97 + s[i]) % P;
        if (i)
        pow = pow * 97 % P;
    }
    sol += Cauta(h1);

    for (i = len; s[i]; i++)
    {
        h1 = ((h1 - s[i - len] * pow % P + P) * 97 + s[i]) % P;
        sol += Cauta(h1);
    }
    fout << sol << "\n";

    return 0;
}