Moderator: Board moderators



/*
* 10420 List of Conquests
* submission 1 RE
* coded at 1004pm march 20, 2006
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PRIME 3001
#define MULTIPLIER 37
#define NHASH PRIME
typedef struct Country Country;
struct Country {
char name[76];
int count;
int next;
};
Country country[3001];
int extra=2003;
void init() {
for(int i=0;i<=PRIME;i++) {
strcpy(country[i].name,"");
country[i].count=0;
country[i].next=-1;
}
}
void print() {
for(int i=0;i<=PRIME;i++) {
if(strcmp(country[i].name,"")!=0) {
printf("%s %d\n",country[i].name,country[i].count);
}
}
}
int compare(const void *va, const void *vb) {
Country *a,*b;
a=(Country *)va;
b=(Country *)vb;
return strcmp(a->name,b->name);
}
unsigned int hash(char *str) {
unsigned int h=0;
int len=strlen(str);
for(int i=0;i<len;i++)
h=MULTIPLIER*h + str[i];
return h % NHASH;
}
void add(char *str) {
int h=hash(str);
if(!strcmp(country[h].name,"")) {
strcpy(country[h].name,str);
country[h].count++;
}else if(!strcmp(country[h].name,str)) {
country[h].count++;
}else {
while(country[h].next!=-1) {
h=country[h].next;
if(!strcmp(country[h].name,str)) {
country[h].count++;
h=-1;
break;
}
}
if(h!=-1) {
country[h].next=++extra;
strcpy(country[extra].name,str);
country[extra].count++;
}
}
}
int main() {
char line[100];
char word[76];
int n;
int i,j;
while(scanf("%d",&n)==1) {
init();
gets(line);
for(i=0;i<n;i++) {
gets(line);
j=0;
while(line[j]!=' ') {
word[j]=line[j];
j++;
}
word[j]='\0';
add(word);
}
qsort(country,PRIME,sizeof(country[0]),compare);
print();
}
}


Users browsing this forum: No registered users and 1 guest