Pagini recente » Statistici Mirea Costin-Alexandru (EuMirea21) | Cod sursa (job #296880) | Monitorul de evaluare | Profil razvandraghici | Cod sursa (job #346370)
Cod sursa(job #346370)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define hash 7000001
//#define hash 71
int a[hash+1];
int cod(char *aux, int len){
int e = 1 , cod = 0;
while (len >0 && *aux){
cod += (e*(int)(*aux - 'a'));
cod = cod %hash;
//cod += ((int)(*aux - 'a'));
e*= 3;
aux ++;
len --;
}
return cod %hash;
}
int main (){
int i;
freopen ("abc2.in","r",stdin);
freopen ("abc2.out","w",stdout);
char *s = (char *)malloc (10000002*sizeof(char));
char *aux = (char *)malloc (21*sizeof(char));
scanf("%s",s);
for(int i=0; i<hash; i++){ a[i]=0;}
i=1;
memset(aux,0,21);
int len;
while( gets(aux) ){
//
i++;
len = strlen(aux);
// printf("%s\n",aux);
a[cod(aux , strlen(aux))]=1;
// printf("codul de la %s:%d\n",aux,cod(aux, strlen(aux)));
// memset(aux,0,21);
// for(int j = 0 ;j<20; j++)
// aux[j]=0;
// scanf("%s\n",aux);
}
int rez = 0;
int indice = cod(s,len);
s += len;
rez += a[indice];
int putere = 1;
for(i=1 ; i < len; i++)
putere *= 3;
while(*s){
indice -= (*(s-len)-'a');
indice = indice / 3;
indice += (*s - 'a')*putere;
indice = indice %hash;
rez += a[indice];
// printf("poz: %s %d\n", s+1 -len, indice);
// if(a[indice])
// printf("%d\n",indice);
s++;
}
printf("%d\n", rez);
// while(1){}
return 0;
}