我只是不想遍历比较那个个字符串数组而已,内存占用可能比较多,不过我相信时间绝对比遍历比较字符串数组要短,CPU占用也少。
不好Free,所以就不手动Free了……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
#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; } |