Cod sursa(job #2831713)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 11 ianuarie 2022 21:55:59
Problema Abc2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
    
#include <bits/stdc++.h>
 
std::ifstream f("abc2.in");
std::ofstream g("abc2.out");
 
const unsigned int MOD = (1 << 14) - 1;
 
std::vector <unsigned int> H[MOD + 1];
 
char s[10000001], word[21];
 
unsigned int len, Len, hash, ans, offset = 1;
 
unsigned int InH(unsigned int hash) {
    const int idx = hash & MOD;
 
    unsigned int N = H[idx].size();
    for(unsigned int i = 0;i < N;i++)
        if(H[idx][i] == hash)
            return 1;
    return 0;
}
 
void addH(unsigned int hash) {
    if(!InH(hash))
        H[hash & MOD].emplace_back(hash);
}
 
void solve() {
    f>> s;
    while(!f.fail()) {
        f >> word;
        len = strlen(word), hash = 0;
        for(unsigned int i = 0;i < len;i++)
            hash = (hash * 3 + (word[i] - 'a'));
        addH(hash);
    }
 
    hash = 0;
    for(unsigned int i = 0;i < len;i++) {
        hash = hash * 3 + (s[i] - 'a');
        offset *= 3;
    }
 
    offset /= 3, Len = strlen(s);
    ans += InH(hash);
 
    for(unsigned i = len;i < Len;i++) {
        hash = (hash - offset * (s[i - len] - 'a')) * 3 + (s[i] - 'a');
        ans += InH(hash);
    }
 
    g << ans;
}
 
int main() {
 
    int T = 1;
    for(;T;T--) {
        solve();
    }
 
    return 0;
}