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); }