Cod sursa(job #3319083)

Utilizator ioanxhIoan Budeanu ioanxh Data 30 octombrie 2025 14:40:03
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ifstream f("abc2.in");
ofstream g("abc2.out");
string s,t;
unordered_map<ll,bool> mp;
const int mod1=1e9+7,mod2=1e9+9,b1=131,b2=37;
int r;
int main(){
    f>>s;
    int l=0;
    while (f>>t) {
        ll h1=0,h2=0;
        int n=t.size();
        for (int i=0; i<n; i++) {
            h1=(h1*b1+(t[i]-'a'))%mod1;
            h2=(h2*b2+(t[i]-'a'))%mod2;
        }
        mp[(h1<<32)^h2]=1;
    }
    l=t.size();
    ll p1=1,p2=1;
    ll h1=0,h2=0;
    for (int i=0; i<l; ++i) {
        h1=(h1*b1+(s[i]-'a'))%mod1;
        h2=(h2*b2+(s[i]-'a'))%mod2;
        if (i>0) {
            p1=(p1*b1)%mod1;
            p2=(p2*b2)%mod2;
        }
    }
    if (mp.count((h1<<32)^h2)) ++r;
    for (int i=l; i<s.size(); ++i) {
        h1=(h1-(s[i-l]-'a')*p1%mod1+mod1)%mod1;
        h1=(h1*b1+(s[i]-'a'))%mod1;

        h2=(h2-(s[i-l]-'a')*p2%mod2+mod2)%mod2;
        h2=(h2*b2+(s[i]-'a'))%mod2;

        if (mp.count((h1<<32)^h2)) ++r;
    }
    g<<r;
    // system("pause");
    return 0;
}