Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Perfect Hash

188.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int n;
char words[13][6];

unsigned int nums[13];
unsigned int C;

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

void
calc(void)
{
	int i,j;
	int done=0;

	C=1;

	while(!done) {
		done=1;
		for(i=0; i<n; i++) {
			for(j=0; j<i; j++) {
				if ((C/nums[i])%n == (C/nums[j])%n) {
					done=0;
					C=MAX(C,MIN(nums[i]*(C/nums[i]+1), nums[j]*(C/nums[j]+1)));
				}
			}
		}
	}
	printf("%u\n\n", C);
}

int
main(void)
{
	char buf[1024];
	int i,j;

	while(fgets(buf, 1024, stdin)) {
		n = sscanf(buf, "%s %s %s %s %s %s %s %s %s %s %s %s %s",
			words[0],words[1],words[2],words[3],words[4],words[5],
			words[6],words[7],words[8],words[9],words[10],words[11],
			words[12]);
		printf("%s", buf);
		for(i=0; i<n; i++) {
			nums[i]=0;
			for(j=0; j<strlen(words[i]); j++) {
				nums[i] <<= 5;
				nums[i] += words[i][j]-'a'+1;
			}
		}
		calc();
	}
	exit(0);
}