題意:根據(jù)輸入的分?jǐn)?shù),打印分?jǐn)?shù)最多的獲勝者抵蚊。如果最高分相同丹皱,則最先獲得最高分的為勝
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node
{
char c;
struct Element *element;
struct Node **next;
} Node;
struct Node *create_node(char c)
{
struct Node *node = malloc(sizeof(struct Node));
node->c = c;
node->next = calloc(26, sizeof(struct Node));
node->element = NULL;
return node;
}
void destroy_node(struct Node *node)
{
int i;
if(node == NULL)
{
return;
}
for(i=0; i<26; i++)
{
destroy_node(node->next[i]);
}
free(node->next);
free(node);
return;
}
typedef struct History
{
int score;
int turn;
struct History * next;
} History;
struct History *create_history(int score, int turn)
{
struct History *history = malloc(sizeof(struct History));
history->score = score;
history->turn = turn;
return history;
}
void destroy_history(struct History *history)
{
if (history->next != NULL) {
destroy_history(history->next);
}
free(history);
}
typedef struct Element
{
char name[33];
int score;
struct Element *next;
struct History *history_curr;
struct History *history_first;
} Element;
struct Element *create_element(char *name, int score)
{
struct Element *element =(Element *)malloc(sizeof(struct Element));
strncpy(element->name, name, 33);
element->score = score;
element->history_curr = NULL;
element->history_first = NULL;
return element;
}
void destroy_element(struct Element *element)
{
if (element->next != NULL) {
destroy_element(element->next);
}
free(element);
}
int main() {
int n;
scanf("%d", &n);
getchar();
struct Node *root = create_node('!');
Element *first = NULL;
Element *curr = NULL;
Element *last = NULL;
int turn = 0;
while(n--) {
struct Node *elem = root;
int c;
int score;
char name[33];
int i = 0;
while ((c = getchar()) != ' ') {
if (c == '\n') continue;
name[i] = c;
c -= 'a';
if(elem->next[c] == NULL) {
elem->next[c] = create_node(c);
}
elem = elem->next[c];
i++;
}
name[i] = '\0';
scanf("%d", &score);
getchar();
if (elem->element == NULL) {
elem->element = create_element(name, 0);
if (first == NULL) {
first = elem->element;
last = elem->element;
} else {
last->next = elem->element;
last = last->next;
}
}
curr = elem->element;
curr->score += score;
if (score >= 0) {
History *ne = create_history(curr->score, turn);
if (curr->history_first == NULL) {
curr->history_first = ne;
} else {
curr->history_curr->next = ne;
}
curr->history_curr = ne;
}
turn++;
}
curr = first->next;
Element *winner = first;
int max_score = winner->score;
while (curr != NULL) {
if (winner->score < curr->score) {
winner = curr;
max_score = winner->score;
} else {
if (winner->score == curr->score) {
int w_turn = turn;
int c_turn = turn;
History *hcurr = winner->history_first;
while (hcurr != NULL) {
if (hcurr->score >= max_score && hcurr->turn < w_turn) {
w_turn = hcurr->turn;
break;
}
hcurr = hcurr->next;
}
hcurr = curr->history_first;
while (hcurr != NULL) {
if (hcurr->score >= max_score && hcurr->turn < c_turn) {
c_turn = hcurr->turn;
break;
}
hcurr = hcurr->next;
}
if (c_turn < w_turn && curr->score == max_score) {
winner = curr;
}
}
}
curr = curr->next;
}
printf("%s", winner->name);
destroy_element(first);
destroy_node(root);
return 0;
}