Pagini recente » Cod sursa (job #1340508) | Cod sursa (job #1082421) | Cod sursa (job #1712003) | Cod sursa (job #803849) | Cod sursa (job #2294196)
#include <cstring>
#include <cstdio>
#include <unordered_set>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
const unsigned int p = 1000009;
int hashString(char *s)
{
unsigned long long hh = 0, p3 = 1;
int i ;
for ( i = strlen(s)-1 ; i >= 0; i-- )
{
hh = (hh + p3 * (s[i]-'0' + 1)) % p;
p3 = (p3*3) % p;
}
return (int)hh;
}
int main()
{
unordered_set <int> dictionar;
char cuv[23], *text;
double t = clock();
FILE *f = fopen("abc2_max.in", "r");
text = (char *)malloc(10000001);
fgets(text, 10000001, f);
int ltext = strlen(text);
if(text[strlen(text) - 1] == '\n')
{
text[strlen(text) - 1] = '\0';
ltext--;
}
int lcuv = 0;
while (fgets(cuv, 23, f) != NULL)
{
if(cuv[strlen(cuv) - 1] == '\n')
cuv[strlen(cuv) - 1] = '\0';
if(lcuv == 0)
lcuv = strlen(cuv);
dictionar.insert(hashString(cuv));
}
fclose(f);
int nr = 0;
for(int i = 0; i <= ltext - lcuv; i++)
{
strncpy(cuv, text + i, lcuv);
cuv[lcuv] = '\0';
if(dictionar.find(hashString(cuv)) != dictionar.end())
nr++;
}
free(text);
f = fopen("abc2.out", "w");
fprintf(f, "%d", nr);
fclose(f);
t = clock() - t;
printf("\nTimp de executare = %.6f\n", t/CLOCKS_PER_SEC);
return 0;
}