Question:http://poj.org/problem?id=1002
问题点:字符映射、选重复项及排序。
1 Memory: 1136K Time: 813MS
2 Language: C++ Result: Accepted
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <iostream>
8
9 using namespace std;
10 const char mp[26]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9'};
11 int mycmp(const void *a,const void *b)
12 {
13 return strcmp((char*)a,(char*)b);
14 }
15 int main()
16 {
17 bool duplicate=false;
18 int len;
19 char c[100];//TIPS:该数组需要足够大的空间
20 char phone[100000][9];//由于字符数组需要'\0'作为结束符,所以增加一位
21 memset(phone,0,sizeof(phone));
22 cin>>len;
23 getwchar();
24 for(int i=0;i<len;i++)
25 {
26 cin>>c;
27 int clen=strlen(c);
28 for(int ch=0,j=0;ch<clen;ch++)
29 {
30 if(c[ch]=='-') continue;
31 if(j==3) phone[i][j++]='-';
32 if(c[ch]>='A'&&c[ch]<'Z') c[ch]=mp[c[ch]-'A'];
33 phone[i][j++]=c[ch];
34 }
35 }
36 qsort(phone,len,sizeof(phone[0]),mycmp);
37 for(int m=0,cnt=1;m<len-1;m++)
38 {
39 if(strcmp(phone[m],phone[m+1])==0)
40 {
41 cnt++;
42 duplicate=true;
43 if(m==len-2) cout<<phone[m]<<" "<<cnt<<endl;
44 }
45 else
46 {
47 if(cnt>1) cout<<phone[m]<<" "<<cnt<<endl;
48 cnt=1;
49 }
50 }
51 if(!duplicate)
52 {
53 cout<<"No duplicates."<<endl;
54 }
55 return 0;
56 }