Cod sursa(job #2294187)

Utilizator gigelmargelgigel margel gigelmargel Data 2 decembrie 2018 00:33:58
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <cstring>
#include <cstdio>
#include <unordered_set>
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

const unsigned int p = 1000000009;


int hashString(char *s)
{
    unsigned long long hh = 0, p3 = 1;
    int i ;
    for ( i = strlen(s)-1 ; i >= 0; i-- )
    {
        hh = (hh + p3 * (s[i]-'0' + 1)) % p;
        p3 = (p3*3) % p;
    }
    return (int)hh;
}

int main()
{
    unordered_set <int> dictionar;

    char cuv[23], ch, *text;

    FILE *f = fopen("abc2.in", "r");

    text = (char *)malloc(10000001);

    fgets(text, 10000001, f);

    int ltext = strlen(text);

    if(text[strlen(text) - 1] == '\n')
    {
        text[strlen(text) - 1] = '\0';
        ltext--;
    }

    int lcuv = 0;

    while (fgets(cuv, 23, f) != NULL)
    {
        if(cuv[strlen(cuv) - 1] == '\n')
            cuv[strlen(cuv) - 1] = '\0';

        if(lcuv == 0)
            lcuv = strlen(cuv);

        dictionar.insert(hashString(cuv));
    }

    fclose(f);

    int nr = 0;
    for(int i = 0; i <= ltext - lcuv; i++)
    {
        strncpy(cuv, text + i, lcuv);
        cuv[lcuv] = '\0';

        if(dictionar.find(hashString(cuv)) != dictionar.end())
            nr++;
    }

    free(text);

    f = fopen("abc2.out", "w");

    fprintf(f, "%d", nr);

    fclose(f);

    return 0;
}