Cod sursa(job #2293746)

Utilizator gigelmargelgigel margel gigelmargel Data 1 decembrie 2018 15:18:50
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <iostream>
#include <cstring>
#include <unordered_set>
#include <iterator>

using namespace std;

const int p = 1000003;

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

int main()
{
    unordered_set <int> dictionar;

    char cuv[23], ch, *text;

    ifstream fin("abc2.in");

    text = new char[10000001];

    int ltext = 0;
    while(fin.get(ch))
    {
        text[ltext++] = ch;
        if(ch == '\n') break;
    }

    fin.get(cuv, 23);

    int lcuv = strlen(cuv);

    dictionar.insert(hashString(cuv));

    fin.get();
    while (fin.get(cuv, 23))
    {
        dictionar.insert(hashString(cuv));
        fin.get();
    }

//    unordered_set<int> :: iterator itr;
//    for (itr = dictionar.begin(); itr != dictionar.end(); itr++)
//        cout << (*itr) << endl;

    fin.close();

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

        strcpy(text, text + 1);

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

    delete []text;

    ofstream fout("abc2.out");

    fout << nr;

    fout.close();

    return 0;
}