Cod sursa(job #595225)

Utilizator SpiderManSimoiu Robert SpiderMan Data 11 iunie 2011 17:10:19
Problema Abc2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
# include <cstdio>
# include <cstring>
# include <ext/hash_set>
using namespace std;
using namespace __gnu_cxx;

const char *FIN = "abc2.in", *FOU = "abc2.out";
const int MAX = 10000005;

char text[MAX], cuv[25];
hash_set < unsigned, hash <int> > H;
int N, L, solution;

inline unsigned codif (char *X) {
    unsigned sol = 0;
    for (int i = 0; i <= L; ++i)
        sol = sol * 3 + X[i] - 'a';
    return sol;
}

int main (void) {
    freopen (FIN, "r", stdin);

    fgets (text, MAX, stdin);
    fgets (cuv, 25, stdin);
    N = strlen (text) - 2, L = strlen (cuv) - 2;
    for (H.insert (codif (cuv)); fgets (cuv, 25, stdin); H.insert (codif (cuv))) ;
    unsigned HASH = 0, piece = 1;
    for (int i = 0; i < L; ++i) {
        HASH = HASH * 3 + text[i] - 'a';
        piece *= 3;
    }
    for (int i = L; i <= N; ++i) {
        HASH = (HASH % piece) * 3 + text[i] - 'a';
        solution += H.count (HASH) ;
    }
    fprintf (fopen (FOU, "w"), "%d", solution) ;
}