Cod sursa(job #1653457)

Utilizator CalarisPredut Denis Stefanita Calaris Data 16 martie 2016 00:24:24
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <vector>
#include <string.h>
#include <iostream>

using namespace std;

const int MAX = 102931;

vector<unsigned int> hashTable[MAX+70];
int length=0;
long long ans = 0;

vector<unsigned int>::iterator operation_search(unsigned int word,unsigned int index)
{
    vector<unsigned int>::iterator it;

    for(it = hashTable[index].begin(); it != hashTable[index].end(); ++it)
        if (*it == word)
            return it;

    return hashTable[index].end();
}

void operation_add(unsigned int word,unsigned int index)
{
    int i;
     if(operation_search(word,index)==hashTable[index].end())
        hashTable[index].push_back(word);
}

int main()
{
    fstream f("abc2.in",ios::in);
    ofstream g("abc2.out");

    string text,word;
    int length2,i,j;
    long long index=0;
    f>>text;

    while(f>>word)
        {
        if(!length)length=word.length();
         for(j=0;j<length;++j)
         index+=(index*3)+(word[j]-'a');
        operation_add(index,index%MAX);
        index = 0;
        }

    length2=text.length()-length+1;

    for(i=0;i<length2;++i)
        {
         index=0;
         for(j=i;j<i+length;++j)
         index+=(index*3)+(text[j]-'a');



           if(operation_search(index,index%MAX)!=hashTable[index].end())
            {
                ++ans;
            }

        }

    g<<ans;
    return 0;
}