Cod sursa(job #1054331)

Utilizator costinbanuCostin Banu costinbanu Data 13 decembrie 2013 18:34:50
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <string>
#include <hash_set>
using namespace std;
using namespace __gnu_cxx;

unsigned pow(unsigned exp) {
    if (exp > 0) return 3u * pow(exp - 1u);
    else return 1;
}

int main() {
    ifstream f("abc2.in");
    ofstream g("abc2.out");
    string text, word;
    hash_set< unsigned , hash<unsigned> > h;
    f >> text >> word;
    int nr = 0, n = word.size();
    unsigned val, maxpow = pow(n - 1);
    do {
        val = 0;
        for (int i = 0; i < word.size(); i++)
            val = val * 3u + (word[i] - 'a');
        h.insert(val);
        f >> word;
    } while (!f.eof());
    for (int i = 0; i < n; i++)
        val = val * 3u + text[i] - 'a';
    nr += (h.find(val) != h.end());
    for (int i = n; i < text.size(); i++) {
        val -= maxpow * (text[i - n] - 'a');
        val = val * 3u + text[i] - 'a';
        nr += (h.find(val) != h.end());
    }
    g << nr;
    f.close();
    g.close();
    return 0;
}