Pagini recente » Cod sursa (job #1189642) | Cod sursa (job #1009048) | Cod sursa (job #176607) | Cod sursa (job #2003103) | Cod sursa (job #2293743)
#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];
// while(fin.get(ch))
// {
// if(ch == '\n') break;
// }
fin.get(text, 10000001);
fin.get();
int ltext = strlen(text);
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++;
}
ofstream fout("abc2.out");
fout << nr;
fout.close();
return 0;
}