Cod sursa(job #2872651)

Utilizator tomaionutIDorando tomaionut Data 17 martie 2022 16:48:12
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>
#define P 15029
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
char s[10000005];
char cuv[30];
vector <int> a[P + 5];
int sol;
bool Cauta(int x)
{
    int r = x % P;
    for (auto w : a[r])
        if (w == x)
            return 1;
    return 0;
}

void Add(int x)
{
    int r = x % P;
    a[r].push_back(x);
}

int main()
{   
    int i, h, pow = 1, len;
    fin >> s;
    while (fin >> cuv)
    {
        h = 0;
        len = strlen(cuv);
        for (i = 0; cuv[i]; i++)
            h = (h * 2 + (cuv[i] - 'a' + 1));
        if (Cauta(h) == 0)
            Add(h);
    }

    h = 0;
    for (i = 0; i < len; i++)
    {
        if (i)
            pow *= 2;
        h = (h * 2 + (s[i] - 'a' + 1));
    }

    sol = Cauta(h);
    for (i = len; s[i]; i++)
    {
        h = (h - pow * (s[i - len] - 'a' + 1)) * 2 + (s[i] - 'a' + 1);
        sol += Cauta(h);
    }

    fout << sol << "\n";

    return 0;
}