#include #include #include
#define MAXBUFSIZE 20 typedef struct phoneNum{ char normal[MAXBUFSIZE]; char standard[MAXBUFSIZE]; }PhoneNum;
int getInput(); void convertToStandard(PhoneNum *pn, int start, int end); char map(char ch); void print(PhoneNum *pn, int start, int end); void sort(PhoneNum *pn, int left, int right); int median(PhoneNum *pn, int left, int right); void swap(PhoneNum *pn, int idx1, int idx2); void printResult(PhoneNum *pn, int start, int end);
int main() { int i, count; PhoneNum *pn; //items of phonenum scanf("%d", &count); if(count <= 0 || count > 100000) return; if((pn = (PhoneNum *)malloc(sizeof(PhoneNum) * count)) == NULL){ printf("allocation error\n"); return; } for(i = 0;i < count;i++) scanf("%s", pn[i].normal); convertToStandard(pn, 0, count - 1); sort(pn, 0, count - 1); printResult(pn, 0, count - 1); }
void convertToStandard(PhoneNum *pn, int start, int end) { int i, j, k; char c; for(i = start;i <= end;i++){ k = 0; for(j = 0;j < strlen(pn[i].normal);j++){ c = pn[i].normal[j]; if(c == '-') continue; if(k == 3) pn[i].standard[k++] = '-'; pn[i].standard[k++] = map(c); } pn[i].standard[k] = '\0'; } } char map(char ch) { static char n; if(ch == '1') n = '1'; else if(ch == 'A' || ch == 'B' || ch == 'C' || ch == '2') n = '2'; else if(ch == 'D' || ch == 'E' || ch == 'F' || ch == '3') n = '3'; else if(ch == 'G' || ch == 'H' || ch == 'I' || ch == '4') n = '4'; else if(ch == 'J' || ch == 'K' || ch == 'L' || ch == '5') n = '5'; else if(ch == 'M' || ch == 'N' || ch == 'O' || ch == '6') n = '6'; else if(ch == 'P' || ch == 'R' || ch == 'S' || ch == '7') n = '7'; else if(ch == 'T' || ch == 'U' || ch == 'V' || ch == '8') n = '8'; else if(ch == 'W' || ch == 'X' || ch == 'Y' || ch == '9') n = '9'; else if(ch == '-') n = '-'; else n = ch; return n; }
void print(PhoneNum *pn, int start, int end) { int i; if(start > end) return; for(i = start;i <= end;i++){ printf("PhoneNum[%d].normal = %s\t", i, pn[i].normal); printf("PhoneNum[%d].standard = %s\n", i, pn[i].standard); } }
void sort(PhoneNum *pn, int left, int right) { if(left + 2 <= right){ int i, j, pivot; pivot = median(pn, left, right); i = left; j = right - 1; while(1){ while(strcmp(pn[++i].standard, pn[pivot].standard) < 0); while(strcmp(pn[--j].standard, pn[pivot].standard) > 0); if(i < j) swap(pn, i, j); else break; } swap(pn, i, right - 1); sort(pn, left, i - 1); sort(pn, i + 1, right); } else if(left + 1 == right){ if(strcmp(pn[left].standard, pn[right].standard) > 0) swap(pn, left, right); } else return; }
int median(PhoneNum *pn, int left, int right) { int center; center = (left + right)/2; if(strcmp(pn[center].standard, pn[left].standard) < 0) swap(pn, center, left); if(strcmp(pn[right].standard, pn[left].standard) < 0) swap(pn, right, left); if(strcmp(pn[right].standard, pn[center].standard) < 0) swap(pn, right, center); swap(pn, center, right - 1); return (right - 1); }
void swap(PhoneNum *pn, int idx1, int idx2) { char normal[MAXBUFSIZE]; char standard[MAXBUFSIZE]; strcpy(normal, pn[idx1].normal); strcpy(standard, pn[idx1].standard); strcpy(pn[idx1].normal, pn[idx2].normal); strcpy(pn[idx1].standard, pn[idx2].standard); strcpy(pn[idx2].normal, normal); strcpy(pn[idx2].standard, standard); } void printResult(PhoneNum *pn, int start, int end) { int i, j, count, flag = 0; for(i = start;i < end;i = j){ count = 1; for(j = i + 1;strcmp(pn[j].standard, pn[i].standard) == 0;j++){ count++; } if(count > 1){ flag = 1; printf("%s %d\n",pn[i].standard, count); } } if(!flag) printf("No duplicates\n"); } |