POJ 1002 487-3279

487-3279
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 295738   Accepted: 52972

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

这题很简单!

下面是把每行数据转换为整型,再排序处理,但是考虑到诸如001-1010,以0开头的号码串,后面输出的时候还要处理一下,不方便!本代码输出时未作处理,不可取!推荐下面第二种!
 1 #include<iostream>
 2 #include<string>
 3 #include<fstream>
 4 #include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     //ifstream in("test.txt");
 9     int ysb[26];
10     ysb[0]=ysb[1]=ysb[2]=2;
11     ysb[3]=ysb[4]=ysb[5]=3;
12     ysb[6]=ysb[7]=ysb[8]=4;
13     ysb[9]=ysb[10]=ysb[11]=5;
14     ysb[12]=ysb[13]=ysb[14]=6;
15     ysb[15]=ysb[17]=ysb[18]=7;
16     ysb[19]=ysb[20]=ysb[21]=8;
17     ysb[22]=ysb[23]=ysb[24]=9;
18     int n;
19     int i,j;
20     string s;
21     cin>>n;
22     int *arr=new int[n];
23     memset(arr,0,sizeof(int)*n);
24     for(i=0;i<n;i++)
25     {
26         cin>>s;
27         int k=1000000;
28         for(j=0;j<s.size();j++)
29         {
30             if(s[j]>='0'&&s[j]<='9')
31             {
32                 arr[i]+=(s[j]-'0')*k;
33                 k/=10;
34             }
35             if(s[j]>='A'&&s[j]<='Y')
36             {
37                 arr[i]+=(ysb[s[j]-65])*k;
38                 k/=10;
39             }
40             if(k==0)
41                 break;
42         }
43     }
44     sort(arr,arr+n);
45     int flag=0;
46     int count=1;
47     for(i=1;i<n;i++)
48     {
49         if(arr[i]==arr[i-1])
50         {
51             count++;    
52             flag=1;
53         }
54         else
55         {
56             if(count>1)
57                 cout<<arr[i-1]/10000<<'-'<<arr[i-1]%10000<<" "<<count<<endl;
58             count=1;
59         }
60     }
61     if(n==1)
62         cout<<"No duplicates.";
63     if(i>=n)
64     {
65         if(flag&&count>1)
66             cout<<arr[i-1]/10000<<'-'<<arr[i-1]%10000<<" "<<count<<endl;
67         else
68             cout<<"No duplicates.";
69     }
70     return 0;
71 }

string字符串版:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<fstream>
 5 #include<algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     //ifstream in("test.txt");
10     char ysb[26];
11     ysb[0]=ysb[1]=ysb[2]='2';
12     ysb[3]=ysb[4]=ysb[5]='3';
13     ysb[6]=ysb[7]=ysb[8]='4';
14     ysb[9]=ysb[10]=ysb[11]='5';
15 
16     ysb[12]=ysb[13]=ysb[14]='6';
17     ysb[15]=ysb[17]=ysb[18]='7';
18     ysb[19]=ysb[20]=ysb[21]='8';
19     ysb[22]=ysb[23]=ysb[24]='9';
20     int n;
21     int i,j;
22     string s;
23     cin>>n;
24     vector<string> arr;
25     string str_temp="12345678";
26     for(i=0;i<n;i++)
27     {
28         cin>>s;
29         int k=0;
30         for(j=0;j<s.size();j++)
31         {
32 
33             if(s[j]>='0'&&s[j]<='9')
34                 str_temp[k++]=s[j];    
35             if(s[j]>='A'&&s[j]<='Y')
36                 str_temp[k++]=ysb[s[j]-65];
37             if(k==3)
38                 str_temp[k++]='-';
39             if(k>=8)
40             {
41                 arr.push_back(str_temp);
42                 break;
43             }
44         }    
45     }
46     sort(arr.begin(),arr.end());
47     int flag=0;
48     int count=1;
49     for(i=1;i<n;i++)
50     {
51         if(arr[i]==arr[i-1])
52         {
53             count++;    
54             flag=1;
55         }
56         else
57         {
58             if(count>1)
59                 cout<<arr[i-1]<<" "<<count<<endl;
60             count=1;
61         }
62     }
63     if(n==1)
64         cout<<"No duplicates.";
65     if(i>=n)
66     {
67         if(flag==1)
68         {
69             if(count>1)
70                 cout<<arr[i-1]<<" "<<count<<endl;
71         }
72         else
73             cout<<"No duplicates.";
74     }
75     return 0;
76 }
View Code


原文地址:https://www.cnblogs.com/zhaopeng938/p/7546181.html