#include <stdio.h> #include <stdlib.h> #include <string.h> //#define __DEBUG #define PHONESIZE 40 typedef struct _phone_stat { char phone[PHONESIZE]; int count; } phone_stat; char CHAR_MAP[0x100]; int count; int nitems = 0; void charmap_init(void) { int i; for (i='0'; i<='9'; ++i) { CHAR_MAP[i] = i; } CHAR_MAP['A'] = '2'; CHAR_MAP['B'] = '2'; CHAR_MAP['C'] = '2'; CHAR_MAP['D'] = '3'; CHAR_MAP['E'] = '3'; CHAR_MAP['F'] = '3'; CHAR_MAP['G'] = '4'; CHAR_MAP['H'] = '4'; CHAR_MAP['I'] = '4'; CHAR_MAP['J'] = '5'; CHAR_MAP['K'] = '5'; CHAR_MAP['L'] = '5'; CHAR_MAP['M'] = '6'; CHAR_MAP['N'] = '6'; CHAR_MAP['O'] = '6'; CHAR_MAP['P'] = '7'; CHAR_MAP['R'] = '7'; CHAR_MAP['S'] = '7'; CHAR_MAP['T'] = '8'; CHAR_MAP['U'] = '8'; CHAR_MAP['V'] = '8'; CHAR_MAP['W'] = '9'; CHAR_MAP['X'] = '9'; CHAR_MAP['Y'] = '9'; CHAR_MAP['-'] = '-'; } void convert(char *dest, char *src) { char c; int i = 0; int j = 0; int k = 0; while((c = src[i++]) != '\0') { char candy = CHAR_MAP[c]; if (candy != '-') { dest[j++] = candy; } if (j == 3) { dest[j++] = '-'; } } dest[j] = '\0'; } void put(phone_stat **items, char *phone_buf) { int i; phone_stat *stat_p; char phone[PHONESIZE]; convert(phone, phone_buf); for (i=0; i<count; ++i) { stat_p = items[i]; if (!stat_p) { stat_p = (phone_stat *)malloc(sizeof(phone_stat)); strcpy(stat_p->phone, phone); stat_p->count = 1; nitems++; items[i] = stat_p; return; } else if (strcmp(stat_p->phone, phone) == 0) { stat_p->count += 1; return; } } } #ifdef __DEBUG char *input[] = { "4873279", "ITS-EASY", "888-4567", "3-10-10-10", "888-GLOP", "TUT-GLOP", "967-11-11", "310-GINO", "F101010", "888-1200", "-4-8-7-3-2-7-9-", "487-3279" }; count = 12; #endif int main(void) { phone_stat *stat; phone_stat **items; phone_stat **gt1; int i, j; int size; int ngt1; charmap_init(); #ifndef __DEBUG scanf("%d", &count); if (count <= 0) { exit(1); } #endif size = sizeof(phone_stat *) * count; items = (phone_stat **)malloc(size); memset(items, 0, size); for (i=0; i<count; ++i) { #ifndef __DEBUG char buf[PHONESIZE]; scanf("%s", buf); put(items, buf); #else put(items, input[i]); #endif } size = sizeof(phone_stat *) * nitems; gt1 = (phone_stat **)malloc(size); j = 0; for (i=0; i<nitems; ++i) { if (items[i]->count > 1) { gt1[j++] = items[i]; } } ngt1 = j; for (i=0; i<ngt1; ++i) { for (j=i+1; j<ngt1; ++j) { if (strcmp(gt1[i]->phone, gt1[j]->phone) > 0) { phone_stat *inter = gt1[i]; gt1[i] = gt1[j]; gt1[j] = inter; } } printf("%s %d\n", gt1[i]->phone, gt1[i]->count); } for (i=0; i<nitems; ++i) { free(items[i]); } free(items); free(gt1); return 0; }
TLE, 考,从这1001,1002两题都发现偶的代码写的太复杂了。需要改进啊,参考了别人的代码,改写成
#include <stdio.h> #include <stdlib.h> #include <string.h> //#define __DEBUG #define PHONESIZE 0x30 #define HTSIZE 10000000 char CHAR_MAP[0x100] = {127}; int count; int nitems = 0; unsigned char phone_hashtable[HTSIZE] = {0}; void charmap_init(void) { int i; for (i='0'; i<='9'; ++i) { CHAR_MAP[i] = i - '0'; } CHAR_MAP['A'] = 2; CHAR_MAP['B'] = 2; CHAR_MAP['C'] = 2; CHAR_MAP['D'] = 3; CHAR_MAP['E'] = 3; CHAR_MAP['F'] = 3; CHAR_MAP['G'] = 4; CHAR_MAP['H'] = 4; CHAR_MAP['I'] = 4; CHAR_MAP['J'] = 5; CHAR_MAP['K'] = 5; CHAR_MAP['L'] = 5; CHAR_MAP['M'] = 6; CHAR_MAP['N'] = 6; CHAR_MAP['O'] = 6; CHAR_MAP['P'] = 7; CHAR_MAP['R'] = 7; CHAR_MAP['S'] = 7; CHAR_MAP['T'] = 8; CHAR_MAP['U'] = 8; CHAR_MAP['V'] = 8; CHAR_MAP['W'] = 9; CHAR_MAP['X'] = 9; CHAR_MAP['Y'] = 9; CHAR_MAP['-'] = '-'; } void convert(int *value, char *src) { char c; int val = 0; int i = 0; while((c = src[i++]) != '\0') { char candy = CHAR_MAP[c]; if (candy >= 0 && candy < 10) { val *= 10; val += candy; } } *value = val; } void printNumber(int number, int count) { printf("%03d-%04d %d\n", number/10000, number % 10000, count); } #ifdef __DEBUG char *input[] = { "0000000", "000-0000", "010-0001", "4873279", "ITS-EASY", "888-4567", "3-10-10-10", "888-GLOP", "TUT-GLOP", "967-11-11", "310-GINO", "F101010", "888-1200", "-4-8-7-3-2-7-9-", "487-3279" }; count = 12; #endif int main(void) { int i; int has_dup = 0; charmap_init(); #ifndef __DEBUG scanf("%d", &count); if (count <= 0) { exit(1); } #endif for (i=0; i<count; ++i) { int value; #ifndef __DEBUG char buf[PHONESIZE]; scanf("%s", buf); convert(&value, buf); #else convert(&value, input[i]); #endif phone_hashtable[value] += 1; } for (i=0; i<HTSIZE; ++i) { if (phone_hashtable[i] > 1) { has_dup = 1; printNumber(i, phone_hashtable[i]); } } if (!has_dup) { printf("No duplicates.\n"); } return 0; }
变成WA, faint 了。