Pagini recente » Cod sursa (job #2986613) | Cod sursa (job #535656) | Cod sursa (job #3190722) | Cod sursa (job #1787407) | Cod sursa (job #113305)
Cod sursa(job #113305)
// 7 teste Ok + 3 tle = 0 pcte
// V1 : Pointeri
#include <stdio.h>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
#define in "abc2.in"
#define out "abc2.out"
#define dim 10000003
#define modulo 1279181
#define ull unsigned long long
int N=0,M;
int S[23];
char Cuvant[dim], linie[23];
// hashing sintax :P
typedef struct hash {
ull vf;
hash* next;
} *PHash;
PHash L[modulo], P;
void Add(ull,int);
bool Search(ull,int);
//
int main()
{
int poz, Total=0;
ull X, H, H1, NR;
bool ok;
FILE *fin = fopen(in,"r");
freopen(out,"w",stdout);
fgets(Cuvant,dim-1,fin);
while ( Cuvant[N] >= 'a' && Cuvant[N] <= 'c' ) N++;
while ( fgets(linie,22,fin) )
{
poz = 0;
while ( linie[poz] >= 'a' && linie[poz] <= 'c' ) poz++;
M = poz;
H = (int)(pow(3,poz-1)), X = 0, poz = 0;
while ( H )
{
X += ((int)linie[poz]-96)*H;
H /= 3, poz++;
}
ok = 0;
NR = (X*2)%modulo;
P = L[NR];
while ( P && !ok )
{
if ( P->vf == X ) ok = 1;
else P = P->next;
}
if ( ok ) continue; // il am deja in hash
Add(X,NR);
}
ull T;
H = H1 = (int)pow(3,M-1);
for ( int i = 0; i <= N-M; i++ )
{
if ( i == 0 )
{
T = 0;
for ( int j = i; j < i+M; j++ )
{
T += ((int)(Cuvant[j]-96))*H, H /= 3;
poz = j;
}
}
else
{
poz += 1;
T -= H1*((int)(Cuvant[i-1]-96));
T *= 3;
T += (int)(Cuvant[poz]-96);
}
Total += Search(T, (T*2)%modulo);
}
printf("%d", Total);
}
bool Search(ull X, int NR)
{
bool ok = 0;
P = L[NR];
while ( P && !ok )
{
if ( P->vf == X ) ok = 1;
else P = P->next;
}
return ok;
}
void Add(ull X, int NR)
{
PHash P = new hash;
P->vf = X;
P->next = L[NR];
L[NR] = P;
}