Cod sursa(job #1302309)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 26 decembrie 2014 20:04:38
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "abc2";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

typedef long long int lld;
const int MOD = 97847;

int N, M, sol;
char S[(int)(1e7) + 5];
char W[25];
vector<lld> H[MOD + 5];

void insert(lld val) {
    lld r = val % MOD;
    for(auto it = H[r].begin(); it != H[r].end(); it++)
        if(*it == val)
            return;
    H[r].push_back(val);
}

bool count(lld val) {
    lld r = val % MOD;
    for(auto it = H[r].begin(); it != H[r].end(); it++)
        if(*it == val)
            return 1;
    return 0;
}

int main() {
    int i;
    lld s, p;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    fgets(S, (1e7) + 5, stdin);

    while(fgets(W, 25, stdin)) {
        if(!N)
            N = strlen(W) - 1;

        for(i = s = 0; i < N; i++)
            s = (s * 3 + W[i] - 'a');

        insert(s);
    }

    M = strlen(S) - 1;

    for(i = s = 0, p = 1; i < N; i++) {
        s = (s * 3 + S[i] - 'a');
        p = (p * 3);
    }

    sol += count(s);

    for(; i < M; i++) {
        s = (s * 3 - p * (S[i - N] - 'a') + S[i] - 'a');
        if(s < 0)
            s = s / 0;
        sol += count(s);
    }

    printf("%d\n", sol);

    return 0;
}