我只是不想遍历比较那个个字符串数组而已,内存占用可能比较多,不过我相信时间绝对比遍历比较字符串数组要短,CPU占用也少。
不好Free,所以就不手动Free了……
#include <stdio.h> #include <string.h> #include <stdlib.h> struct balloon_index { struct balloon_index *a; struct balloon_index *b; struct balloon_index *c; struct balloon_index *d; struct balloon_index *e; struct balloon_index *f; struct balloon_index *g; struct balloon_index *h; struct balloon_index *i; struct balloon_index *j; struct balloon_index *k; struct balloon_index *l; struct balloon_index *m; struct balloon_index *n; struct balloon_index *o; struct balloon_index *p; struct balloon_index *q; struct balloon_index *r; struct balloon_index *s; struct balloon_index *t; struct balloon_index *u; struct balloon_index *v; struct balloon_index *w; struct balloon_index *x; struct balloon_index *y; struct balloon_index *z; int count; }; struct balloon_index *struct_inital(); int main() { struct balloon_index *start, *pointer1, **pointer2; int amount, i, ii, len, max_count; char color[16], max[16]; while (1) { scanf("%d", &amount); if (amount == 0) break; start = struct_inital(); max_count = 0; for (i = 1; i <= amount; i++) { scanf("%s", color); pointer1 = start; len = strlen(color); for (ii = 0; ii <= len; ii++) { switch (color[ii]) { case 'a': pointer2 = &pointer1->a; pointer1 = pointer1->a; break; case 'b': pointer2 = &pointer1->b; pointer1 = pointer1->b; break; case 'c': pointer2 = &pointer1->c; pointer1 = pointer1->c; break; case 'd': pointer2 = &pointer1->d; pointer1 = pointer1->d; break; case 'e': pointer2 = &pointer1->e; pointer1 = pointer1->e; break; case 'f': pointer2 = &pointer1->f; pointer1 = pointer1->f; break; case 'g': pointer2 = &pointer1->g; pointer1 = pointer1->g; break; case 'h': pointer2 = &pointer1->h; pointer1 = pointer1->h; break; case 'i': pointer2 = &pointer1->i; pointer1 = pointer1->i; break; case 'j': pointer2 = &pointer1->j; pointer1 = pointer1->j; break; case 'k': pointer2 = &pointer1->k; pointer1 = pointer1->k; break; case 'l': pointer2 = &pointer1->l; pointer1 = pointer1->l; break; case 'm': pointer2 = &pointer1->m; pointer1 = pointer1->m; break; case 'n': pointer2 = &pointer1->n; pointer1 = pointer1->n; break; case 'o': pointer2 = &pointer1->o; pointer1 = pointer1->o; break; case 'p': pointer2 = &pointer1->p; pointer1 = pointer1->p; break; case 'q': pointer2 = &pointer1->q; pointer1 = pointer1->q; break; case 'r': pointer2 = &pointer1->r; pointer1 = pointer1->r; break; case 's': pointer2 = &pointer1->s; pointer1 = pointer1->s; break; case 't': pointer2 = &pointer1->t; pointer1 = pointer1->t; break; case 'u': pointer2 = &pointer1->u; pointer1 = pointer1->u; break; case 'v': pointer2 = &pointer1->v; pointer1 = pointer1->v; break; case 'w': pointer2 = &pointer1->w; pointer1 = pointer1->w; break; case 'x': pointer2 = &pointer1->x; pointer1 = pointer1->x; break; case 'y': pointer2 = &pointer1->y; pointer1 = pointer1->y; break; case 'z': pointer2 = &pointer1->z; pointer1 = pointer1->z; break; case '\0': pointer1->count++; if (pointer1->count >= max_count) { max_count = pointer1->count; strcpy(max, color); } } if (pointer1 == NULL) { *pointer2 = struct_inital(); pointer1 = *pointer2; } } } printf("%s\n", max); } return 0; } struct balloon_index *struct_inital() { struct balloon_index *index; index = (struct balloon_index *) malloc(sizeof(struct balloon_index)); index->a = index->b = index->c = index->d = index->e = index->f = index->g = index->h = index->i = index->j = index->k = index->l = index->m = index->n = index->o = index->p = index->q = index->r = index->s = index->t = index->u = index->v = index->w = index->x = index->y = index->z = NULL; index->count = 0; return index; }