Cod sursa(job #1582412)

Utilizator japjappedulapPotra Vlad japjappedulap Data 27 ianuarie 2016 21:43:57
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;

ofstream os ("abc2.out");

char Main[10000031];
char Word[22];
int L;

const int MOD = 90907;

vector <int> Hash[MOD];

void Input();
void Insert(int );
bool Existence(int );

int main()
{
    Input();
    int sol = 0, x = 0;

    for (char *i = Main, *j; *(i+L-1) != '\0'; ++i, x = 0)
    {
        j = i;
        for (int k = L; k > 0; --k, ++j)
            x = (x * 3 + *j - 'a');
        sol += Existence(x);
    }


    os << sol;
    os.close();
}

void Input()
{
    ifstream is ("abc2.in");
    is >> Main;


    for (int x = 0; is >> Word; Insert(x), x = 0)
        for (char *p = Word; *p != '\0'; ++p)
            x = (x * 3 + *p-'a');
    L = strlen(Word);

    is.close();
};

void Insert(int X)
{
    if (Existence(X)) return;
    Hash[X % MOD].push_back(X);
};

bool Existence(int X)
{
    for (const int& i : Hash[X % MOD])
        if (i == X)
            return true;
    return false;
};