Cod sursa(job #1302273)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 26 decembrie 2014 19:38:14
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#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;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = 666013;

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

void insert(unsigned int val) {
    int r = val % MOD;
    for(auto x : H[r])
        if(x == val)
            return;
    H[r].push_back(val);
}

bool count(unsigned int val) {
    int r = val % MOD;
    for(auto x : H[r])
        if(x == val)
            return 1;
    return 0;
}

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

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

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

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

        s = 0;

        for(i = 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');
        sol += count(s);
    }

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

    return 0;
}