注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

长风明志的博客

不要也不能做下一个谁,应该且可以做第一个自己

 
 
 

日志

 
 

PKU 第1002题 487-3279  

2011-05-07 19:45:26|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

【问题】http://poj.org/problem?id=1002

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 159160 Accepted: 27174
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

12  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  

Sample Output

310-1010 2  487-3279 4  888-4567 3

/*

   544k   438ms

*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
void QuickSort(int* output,int left,int right)     //quick sort the outputed data
{
/*                                                              //注释部分为被优化的代码,原先时间超了
   int i=start,j=end,temp;
   if(end-start<1)  return;              //only one ,needn't compare
   temp=output[start];
   while(i<j)
   {
     while(i<j&&temp<=output[j]) j--;
     if(i<j) { output[i]=output[j];  i++; }
     while(i<j&&output[i]<temp) i++;
     if(i<j) { output[j]=output[i];  j--; }
   }
   output[i]=temp;
   QuickSort(output,start,i-1);
   QuickSort(output,i+1,end);
   */
   if(left<right)
  {
    int i=left,j=right,t,p=output[left];
    do
    {
      while(output[i]<p)
        i++;
      while(output[j]>p)
        j--;
      if(i<=j)
      {
        t=output[i];
        output[i]=output[j];
        output[j]=t;
        i++;
        j--;
      }
    }
    while(i<=j);
    QuickSort(output,left,j);
    QuickSort(output,i,right);
  }
}

int handleInput(char *input)     //translate the input datas into standard phone number
{
    int i,j=0,index=0,output=0;
    char hash[] = "22233344455566670778889990"; 
        while(j<strlen(input))
        {
           if(input[j]>='0'&&input[j]<='9')          //'0'~'9'
           {
               output=output*10+(input[j]-'0');
               index++;
               if(index==7)  break;
           }
           else if(input[j]!='Q'&&input[j]!='Z'&&input[j]>='A'&&input[j]<='Z')         //'A'~'Z' exclude 'Q'and'Z'
           {
               output=output*10+(hash[input[j]-'A']-'0');
               index++;
               if(index==7) break;
           }
           j++;
        }
        if(index<7)
        {
           for(i=1;i<=7-index;i++)
               output*=10;
        }
   return output;     
}
int main()
{
   char temp[100];
   int i,count,flag=0,duplicate,*input;
   scanf("%d",&count);
   input=(int *)malloc(sizeof(int)*count);
   for(i=0;i<count;i++)         //input the data
   {
      scanf("%s",temp);
      *(input+i)=handleInput(temp);
   }
   QuickSort(input,0,count-1);
   i=0;
    while(i<count)
    {
       duplicate=1;
       while((i+1)<count&&input[i]==input[i+1])       //have duplicates
       {
          duplicate++;
          i++;
          flag=1;
       }
       if(duplicate>1) printf("%03d-%04d %d\n",input[i]/10000,input[i]%10000,duplicate);
       i++;
    }
    if(!flag)          //have no duplicates
       printf("No duplicates.\n");
   free(input);
   return 1;
}

  评论这张
 
阅读(199)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017