比赛 20111110 评测结果 AAWWWWWWWW
题目名称 韩国明星 最终得分 20
用户昵称 fanzeyi 运行时间 0.000 s
代码语言 C 内存使用 0.00 MiB
提交时间 2011-11-10 11:20:23
显示代码纯文本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100010

typedef struct {
    char name[100];
    int heart;
} People;

People star[MAX];
int n, p;

unsigned int SDBMHash(char *str) {
    unsigned int hash = 0;
    while (*str){
        // equivalent to: hash = 65599*hash + (*str++);
        hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    }
    return (hash & 0x7FFFFFFF) % MAX;
}

int cmp(const void *a, const void *b) {
    People *c = (People*)a;
    People *d = (People*)b;
    if(c->name[0] == '\0' && d->name[0] == '\0') {
        return 0;
    }else if(c->name[0] != '\0' && d->name[0] == '\0') {
        return 1;
    }else if(d->name[0] != '\0' && c->name[0] == '\0') {
        return 0;
    }
    return d->heart - c->heart;
}

int main(int argc, char const *argv[]) {
    FILE *fin = fopen("star.in", "r");
    FILE *fout = fopen("star.out", "w");
    int i;
    int hash;
    int change;
    char tmp[100];
    memset(star, 0, sizeof(star));
    fscanf(fin, "%d", &n);
    for(i = 0; i < n; i++) {
        fscanf(fin, "%s", tmp);
        hash = SDBMHash(tmp);
        strcpy(star[hash].name, tmp);
    }
    fscanf(fin, "%d", &p);
    for(i = 0; i < p; i++) {
        fscanf(fin, "%s\n%d", tmp, &change);
        hash = SDBMHash(tmp);
        star[hash].heart += change;
    }
    qsort(star, MAX, sizeof(star[0]), cmp);
    for(i = MAX - n; i < MAX; i++) {
        fprintf(fout, "%s\n%d\n", star[i].name, star[i].heart);
    }
    return 0;
}