Created
March 8, 2011 22:18
-
-
Save iporsut/861212 to your computer and use it in GitHub Desktop.
gourmet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <string.h> | |
#define MAX 21 | |
typedef struct _list { | |
int n; | |
struct _list *next; | |
}List; | |
int length[MAX]; | |
int slot[MAX]; | |
List *table[MAX]; | |
int N; | |
void clear_mem() { | |
int i; | |
List *node; | |
for(i = 0; i < N; ++i) { | |
while(table[i] != NULL) { | |
node = table[i]; | |
table[i] = table[i]->next; | |
node->next = NULL; | |
free(node); | |
} | |
} | |
} | |
void parse() { | |
char line_input[80]; | |
char *tok; | |
List *node; | |
int num; | |
int count; | |
int i; | |
fgets(line_input,80,stdin); | |
sscanf(line_input,"%d",&N); | |
for (i = 0; i < N; ++i) { | |
fgets(line_input,80,stdin); | |
tok = strtok (line_input," \n"); | |
count = 0; | |
while(tok != NULL) { | |
num = atoi(tok); | |
if ((table[num-1] == NULL) || ((table[num-1] != NULL) && (table[num-1]->n != i))) { | |
count++; | |
node = (List*)malloc(sizeof(List)); | |
node->n = i; | |
node->next = table[num-1]; | |
table[num-1] = node; | |
} | |
tok = strtok(NULL, " \n"); | |
} | |
length[i] = count; | |
} | |
} | |
void find_day() { | |
int i,min = MAX,pos = -1; | |
List *node; | |
for (i = MAX-1; i >= 0; --i) { | |
node = table[i]; | |
while(node != NULL) { | |
if((slot[node->n] == 0) && (length[node->n] < min)) { | |
min = length[node->n]; | |
pos = node->n; | |
} | |
node = node->next; | |
} | |
if (pos != -1) { | |
slot[pos] = i+1; | |
pos = -1; | |
min = MAX; | |
} | |
} | |
} | |
int main(int argc,char *argv[]) { | |
int i; | |
parse(); | |
find_day(); | |
for(i = 0; i < N; ++i) { | |
printf("%d ",slot[i]); | |
} | |
printf("\n"); | |
clear_mem(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment