POJ 487-3279解题总结

来源:互联网 发布:mysql 重新加载配置 编辑:程序博客网 时间:2024/06/03 11:44

487-3279
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 224085 Accepted: 39019

Description

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10. 

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: 

A, B, and C map to 2 
D, E, and F map to 3 
G, H, and I map to 4 
J, K, and L map to 5 
M, N, and O map to 6 
P, R, and S map to 7 
T, U, and V map to 8 
W, X, and Y map to 9 

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. 

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) 

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. 

Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. 

Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: 

No duplicates. 

Sample Input

124873279ITS-EASY888-45673-10-10-10888-GLOPTUT-GLOP967-11-11310-GINOF101010888-1200-4-8-7-3-2-7-9-487-3279

Sample Output

310-1010 2487-3279 4888-4567 3

Source

East Central North America 1999


#include"stdafx.h"#include<string.h>#include<stdio.h>#include<cstring>#include<cstdlib>#include<cstdio>void to_StandardForm(char *p, int n);void find_SameTel(int n);void sort_Tel(int n);void quicksort(int start, int end);
//其实不需要结构,定义也个二维数组就可以了,主要是我还定义的一些其他字段,开始时没有想到字符串排序,北=悲剧啊struct telephone{char telnumber[10];   //电话号码};struct telephone tel[100005];int main(){int n;int i = 0,j,k,count=1;char str[1000];int flag = 0;scanf("%d",&n);k = n;while(k-- > 0){scanf("%s",str);to_StandardForm(str,i);i ++;}//strcpy(tel[i].telnumber,"error");sort_Tel(n);for(j = 1; j < n; j ++ ){if(!strcmp(tel[j].telnumber,tel[j-1].telnumber)){flag = 1;count ++;}else{if(count > 1){printf("%s %d\n",tel[j-1].telnumber,count);}count = 1;}}
if(count > 1) //查看最后一条记录是否有重复{printf("%s %d\n", tel[j-1].telnumber, count);}if(!flag){printf("No duplicates.");}return 0;}//将电话号码转换为标准形式void to_StandardForm(char *p, int n){char *q;char ch,s[10];int i = 0, j = 0;q = p;while(*q != '\0'){ch = *q;if(j == 3){s[j] = '-';j ++;}switch(ch){case '0': s[j] = '0';break;case '1': s[j] = '1';break;case '2':case 'A':case 'B':case 'C': s[j] = '2';break;case '3':case 'D':case 'E':case 'F': s[j] = '3';break;case '4':case 'G':case 'H':case 'I': s[j] = '4';break;case '5':case 'J':case 'K':case 'L': s[j] = '5';break;case '6':case 'M':case 'N':case 'O': s[j] = '6';break;case '7':case 'P':case 'Q':case 'R':case 'S': s[j] = '7';break;case '8':case 'T':case 'U':case 'V': s[j] = '8';break;case '9':case 'W':case 'X':case 'Y': case 'Z':s[j] = '9';break;default: if(j>=0) j -- ; break;}j++;*q++;}s[j] = '\0';strcpy(tel[n].telnumber,s);}int comp(const void *p,const void *q){return strcmp(((struct telephone*)p)->telnumber , ((struct telephone*)q)->telnumber);}void sort_Tel(int n){//采用快速排序 qsort(tel,n,sizeof(tel[0]),comp);//quicksort(0,n-1);}
//这是自己写的快排函数,不知为啥,总是Time Limit Exceeded,所以调用了系统自带的快排函数,关于快排函数有以下几点需要注意void quicksort(int start, int end){struct telephone tem;int i, j;i = start;j = end;tem = tel[start];if(start < end){while(i< j){while(i<j && (strcmp(tem.telnumber,tel[j].telnumber) < 0) )j--;if(i<j){tel[i] = tel[j];  //将小于基准值的tel[j]放到tel[i]的位置上i++;              //位置右移}while(i<j && (strcmp(tem.telnumber,tel[i].telnumber) >= 0))i ++;if(i < j){tel[j] = tel[i];j--;}}tel[i] = tem;quicksort(start, j - 1);quicksort(j + 1, end);}}

系统快排函数:
qsort(s,N,sizeof(s[0]),cmp);
int cmp(const void *a,const void *b)
{
     return *(int *)a- *(int *)b;  //升序排序
     return *(int *)b- *(int *)a;   //降序排序
}

1、对int数组排序:
int cmp(const void *a,const void *b)
{
     return *(int *)a- *(int *)b;  //升序排序
     return *(int *)b- *(int *)a;   //降序排序
}

2、对double型数组排序
int cmp(const void * a, const void *b)
{
       return((*(double*)a-*(double*)b>0)?1:-1);
}
/*注意这里不能像上面对int型数组排序时那样直接返回*(double*)a-*(double*)b,因为这个cmp函数的返回值已经规定了是int型,而*(double*)a-*(double*)b是double型,这里是对这个double型数组进行了升序排列,如果return((*(double*)b-*(double*)a>0)?1:-1)或者return((*(double*)a-*(double*)b>0)?-1:1)则对数组进行降序排列*/   
  (原文:http://wenku.baidu.com/view/3567c018964bcf84b9d57b2f.html)

3、对字符数组排序
int cmp(const void *a,const void *b)
{
     return *(char *)a- *(char *)b;  //升序排序
     return *(char *)b- *(char *)a;   //降序排序
}

4、对字符串数组进行排序
int cmp(const void *a,constvoid*b)
{
        return(strcmp((char*)a,(char*)b));
}