sdut 487-3279【哈希查找,sscanf ,map】

487-3279

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

题目链接:

sdut:   http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1001

poj:    http://poj.org/problem?id=1002

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.

输入

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.

输出

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.

示例输入

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

示例输出

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

题目大意:给出大写字母和数字,按照一定的规则映射到电话号码(7位),查找所有的的电话号码,若是有重复,则先按照xxx-xxxx的格式输出电话号码(按照字典序),紧接着一个空格,输出重复的次数;要注意,字母Q和Z不会出现在输入的字符串中,而且,给出的“伪号码”的个数最多可以到100000个。
解题思路:由于伪号码的数量可能会非常大,如果定义二维数组保存伪号码的话,或许会容易导致超内存,最好的法子是使用哈希查找的方法,用一个数组hash[]保存重复的次数,下标保存电话号码,由于电话号码最长是7位,所以要定义一个10000000大的数组,就是hash[10000000],保存的时候就已经自动排完序了,所以省去了排序的步骤,效率相应也会比较高,最后遍历输出满足要求的电话号码和重复次数即可。(注意,若是使用c++的话无法运行程序,好像是超内存了,改成c就能正常运行了,真心不知道什么原因)
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int hash[10000000]={0};
 4 int zhuanzhi(int n)
 5 {
 6     int sum=1,i;
 7     for(i=1;i<n;i++)
 8         sum*=10;
 9     return sum;
10 }
11 int zhuanhuan(char f[])
12 {
13     int sum=0,i;
14     int t=strlen(f);
15     for(i=t-1;i>=0;i--)
16     {
17         sum=sum+(f[i]-'0')*zhuanzhi(t-i);
18     }
19     return sum;
20 }
21 int main()
22 {
23     int zong,i,j;
24     scanf("%d",&zong);
25     while(zong--)
26     {
27         char f[100],g[100];
28         scanf("%s",f);
29         int s=-1;
30         for(i=0;f[i]!='\0';i++)
31         {
32             switch(f[i])
33             {
34             case '0':
35                 g[++s]='0';
36                 break;
37             case '1':
38                 g[++s]='1';
39                 break;
40             case 'A':case 'B':case 'C':case '2':
41                 g[++s]='2';
42                 break;
43             case 'D':case 'E':case 'F':case '3':
44                 g[++s]='3';
45                 break;
46             case 'G':case 'H':case 'I':case '4':
47                 g[++s]='4';
48                 break;
49             case 'J':case 'K':case 'L':case '5':
50                 g[++s]='5';
51                 break;
52             case 'M':case 'N':case 'O':case '6':
53                 g[++s]='6';
54                 break;
55             case 'P':case 'R':case 'S':case '7':
56                 g[++s]='7';
57                 break;
58             case 'T':case 'U':case 'V':case '8':
59                 g[++s]='8';
60                 break;
61             case 'W':case 'X':case 'Y':case '9':
62                 g[++s]='9';
63                 break;
64             }
65         }
66         g[++s]='\0';
67         hash[zhuanhuan(g)]++;
68     }
69     int count=0;
70     for(i=0;i<10000000;i++)
71         if(hash[i]>1)
72         {
73             printf("%03d-%04d %d\n",i/10000,i%10000,hash[i]);
74             count++;
75         }
76     if(count==0)printf("No duplicates.\n");
77     return 0;
78 }
View Code

 方法二:用c++标准模板库关联容器map做,在sdut上ac了,但是在poj上超时了,不知道问题出在哪里。问题找到了,交的时候要选择“c++”,而默认的提交方式是“g++”,所以要注意这一点才行。

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     cin>>n;
 9     map<string,int>mapx;
10     pair<map<string,int>::iterator,bool>pairx;
11     int i;
12     while(n--)
13     {
14         string f,temp("");
15         cin>>f;
16         int flag=0;
17         for(i=0; i<=f.length(); i++)
18         {
19             if(flag==3)
20             {
21                 temp.append("-");
22                 flag++;
23             }
24             if(f[i]=='A'||f[i]=='B'||f[i]=='C'||f[i]=='2')
25             {
26                 temp.append("2");
27                 flag++;
28             }
29             else if(f[i]=='D'||f[i]=='E'||f[i]=='F'||f[i]=='3')
30             {
31                 temp.append("3");
32                 flag++;
33             }
34             else if(f[i]=='G'||f[i]=='H'||f[i]=='I'||f[i]=='4')
35             {
36                 temp.append("4");
37                 flag++;
38             }
39             else if(f[i]=='J'||f[i]=='K'||f[i]=='L'||f[i]=='5')
40             {
41                 temp.append("5");
42                 flag++;
43             }
44             else if(f[i]=='M'||f[i]=='N'||f[i]=='O'||f[i]=='6')
45             {
46                 temp.append("6");
47                 flag++;
48             }
49             else if(f[i]=='P'||f[i]=='R'||f[i]=='S'||f[i]=='7')
50             {
51                 temp.append("7");
52                 flag++;
53             }
54             else if(f[i]=='T'||f[i]=='U'||f[i]=='V'||f[i]=='8')
55             {
56                 temp.append("8");
57                 flag++;
58             }
59             else if(f[i]=='W'||f[i]=='X'||f[i]=='Y'||f[i]=='9')
60             {
61                 temp.append("9");
62                 flag++;
63             }
64             else if(f[i]=='0')
65             {
66                 temp.append("0");
67                 flag++;
68             }
69             else if(f[i]=='1')
70             {
71                 temp.append("1");
72                 flag++;
73             }
74         }
75         pairx=mapx.insert(pair<string,int>(temp,0));
76         if(pairx.second==0)
77         {
78             ++pairx.first->second;
79         }
80         else continue;
81     }
82     int flag2=0;
83     map<string,int>::iterator p;
84     for(p=mapx.begin(); p!=mapx.end(); p++)
85     {
86         if(p->second==0)
87             continue;
88         else
89         {
90             cout<<p->first<<" "<<p->second+1<<endl;
91             flag2=1;
92         }
93     }
94     if(flag2==0)
95     {
96         cout<<"No duplicates."<<endl;
97     }
98     return 0;
99 }
View Code

 

 
posted @ 2013-11-04 22:43  狂盗一枝梅  阅读(282)  评论(0编辑  收藏  举报