Pagini recente » Cod sursa (job #25986) | Cod sursa (job #3190107) | Cod sursa (job #1767108) | Cod sursa (job #1960596) | Cod sursa (job #1654237)
#include <fstream>
#include <vector>
#include <string.h>
#include <iostream>
using namespace std;
const int MAX = 666013;
vector<unsigned int> hashTable[MAX+70];
int length=0;
int ans = 0;
vector<unsigned int>::iterator operation_search(unsigned int number,unsigned int index)
{
vector<unsigned int>::iterator it;
for(it = hashTable[index].begin(); it != hashTable[index].end(); ++it)
if (*it == number)
return it;
return hashTable[index].end();
}
void operation_add(string word)
{
int i;
unsigned int index = 0;
for(i=0;i<length;++i)
index=(index*3)+(word[i]-'a');
if(operation_search(index,index%MAX)==hashTable[index%MAX].end())
hashTable[index%MAX].push_back(index);
}
int main()
{
fstream f("abc2.in",ios::in);
ofstream g("abc2.out");
string text,word,temp;
int length2,i,j;
unsigned int 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)
{
for(j=i;j<i+length;++j)
index=(index*3)+(text[j]-'a');
if(operation_search(index,index%MAX)!=hashTable[index%MAX].end())
{
++ans;
}
index=0;
}
g<<ans;
return 0;
}