Cod sursa(job #1653436)

Utilizator CalarisPredut Denis Stefanita Calaris Data 15 martie 2016 23:36:22
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <vector>
#include <string.h>
#include <iostream>

using namespace std;

const int MAX = 666013;

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

vector<string>::iterator operation_search(string word,long long index)
{
    vector<string>::iterator it;

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

    return hashTable[index].end();
}

void operation_add(string word)
{
    int i;
    long long index = 0;
    for(i=0;i<length;++i)
        index+=(index*3)+(word[i]-'a');
    index = index%MAX;
     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,temp;
    int length2,i,j;
    long long index=0;
    f>>text;

    while(f>>word)
        {
        if(!length)length=word.length();
        operation_add(word);
        }

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

    for(i=0;i<length2;++i)
        {
         temp.append(text,i,length);

         for(j=0;j<length;++j)
         index+=(index*3)+(temp[j]-'a');

         index = index%MAX;

         if(operation_search(temp,index)!=hashTable[index].end())
            {
                ++ans;
            }
         temp.clear();
         index=0;
        }

    g<<ans;
    return 0;
}