Cod sursa(job #2297079)

Utilizator mircearoataMircea Roata Palade mircearoata Data 5 decembrie 2018 12:06:51
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda prega_casi_5.12.2018 Marime 0.93 kb
#include <fstream>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

ifstream in("abc2.in");
ofstream out("abc2.out");

int cnt, n, len, ans;
string ch;
string cuv;
unordered_set<long long> hashes;
long long h, put3 = 1;

int main()
{
    ios_base::sync_with_stdio(false);
    getline(in, ch);
    while(!in.eof())
    {
        getline(in, cuv);
        n = max(n, (int)cuv.length());
        h = 0;
        for(int i = 0; i < n; i++)
            h += h + h + cuv[i] - 'a';
        hashes.insert(h);
    }
    len = ch.length();
    h = 0;
    for(int i = 0; i < n; i++)
    {
        h += h + h + ch[i] - 'a';
        put3 *= 3;
    }
    put3 /= 3;
    for(int i = n; i < len; i++)
    {
        ans += hashes.count(h);
        while(h >= put3)
            h -= put3;
        h += h + h + ch[i] - 'a';
    }
    ans += hashes.count(h);
    out << ans;
    return 0;
}