Pagini recente » Cod sursa (job #459665) | tema | Cod sursa (job #1661194) | Cod sursa (job #933687) | Cod sursa (job #796862)
Cod sursa(job #796862)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define HASH_BASE 101
#define MAXS 2000001
int init_hash(char* low, char* up) {
int hash = 0;
int base = 1;
while(up > low) {
up--;
hash += base * (*up);
base *= HASH_BASE;
}
return hash;
}
int roll_hash(int oldv, int hash, int newv, int exp) {
hash -= oldv*pow(HASH_BASE, exp-1);
hash *= HASH_BASE;
hash += newv;
return hash;
}
int main() {
char *a;
char *b;
int M, N, nr_matches = 0, poz[1000];
int a_hash, sub_hash;
int i, max;
FILE *f;
f = fopen("strmatch.in","rt");
a = malloc(MAXS*sizeof(char));
b = malloc(MAXS*sizeof(char));
fgets(a, MAXS, f);
fgets(b, MAXS, f);
fclose(f);
M = strlen(a);
N = strlen(b);
a_hash = init_hash(a,a+M);
sub_hash = init_hash(b,b+M);
for(i = 0; i < N-M+1; i++) {
if(a_hash == sub_hash) {
nr_matches++;
if(nr_matches <= 1000)
poz[nr_matches-1] = i;
}
sub_hash = roll_hash(b[i], sub_hash, b[i+M], M);
}
free(a);
free(b);
f = fopen("strmatch.out", "wt");
fprintf(f,"%i\n",nr_matches);
max = 1000 < nr_matches? 1000: nr_matches;
for(i = 0; i < max; i++)
fprintf(f,"%i ",poz[i]);
fclose(f);
}