POJ1002-487-3279

转载请注明出处:優YoU  http://user.qzone.qq.com/289065406/blog/1306848752

大致题意:

中文题,我就不废话了,不过据说某些RP低的同学会看到本题是英文题。。。

 

解题思路:

 

有两种处理方法:

一、Hash+qsort

在输入时把字符号码转换为7位数字,用int保存,然后开两个8位数组visttime,分别记录该号码是否出现过;若出现过,出现的次数是多少。

把出现过2次或以上的号码先逐一存放到待输出数组sort_out

输入完毕后,对数组sort_out快排,逐一输出这些号码及其出现次数即可。

 

二、qsort

在输入时先把字符号码全部转换为7位数字,然后全部存入数组sort_out

qsort对数组sort_out排序,然后顺次读取每一个数组sort_out中的号码,相同的号码必然是连续存放的,只需把连续出现2次以上的号码输出即可。

 

 

注意:本题输入必须使用字符串输入,若逐字输入,无论用何种算法都会超时。

 

sort()与qsort( ) 都是C++的快排函数,本题的排序机制很简单,不想写比较函数的同学可以用sort()

 

 1 //Memory Time
2 //49324K 907MS
3
4 /*Hash+Qsort*/
5
6 #include<iostream>
7 #include<algorithm>
8 #include<iomanip>
9 using namespace std;
10
11 int ctoi(char ch) //把字符ch转换为其在手机上对应的数字键
12 {
13 if(ch=='A' || ch=='B' || ch=='C')
14 return 2;
15 if(ch=='D' || ch=='E' || ch=='F')
16 return 3;
17 if(ch=='G' || ch=='H' || ch=='I')
18 return 4;
19 if(ch=='J' || ch=='K' || ch=='L')
20 return 5;
21 if(ch=='M' || ch=='N' || ch=='O')
22 return 6;
23 if(ch=='P' || ch=='R' || ch=='S')
24 return 7;
25 if(ch=='T' || ch=='U' || ch=='V')
26 return 8;
27 if(ch=='W' || ch=='X' || ch=='Y')
28 return 9;
29 }
30
31 int time[10000000]; //各个号码出现的次数
32 bool vist[10000000]; //各个号码出现的次数
33 int sort_out[100000]; //按字典序存放待输出的电话号码
34
35 int main(void)
36 {
37 int n; //号码数
38 while(cin>>n)
39 {
40 /*Initial*/
41
42 memset(time,0,sizeof(time));
43 memset(vist,false,sizeof(vist));
44 int ps=0; //sort_out指针
45 bool flag=false; //标记是否出现过重复号码
46
47 /*Input*/
48
49 for(int i=1;i<=n;i++)
50 {
51 int x=0;
52 char s[20];
53 cin>>s;
54 for(int j=0;s[j]!='\0';j++)
55 {
56 if(s[j]=='-' || s[j]=='Q' || s[j]=='Z')
57 continue;
58 else if(s[j]<='9')
59 x=x*10+s[j]-'0';
60 else if(s[j]<='Z')
61 x=x*10+ctoi(s[j]);
62 }
63
64 time[x]++;
65
66 if(!vist[x] && time[x]>=2) //电话号码x重复出现2次以上,则等待输出
67 {
68 flag=true;
69 vist[x]=true;
70 sort_out[ps++]=x;
71 }
72 }
73
74 /*Sort & Output*/
75
76 if(!flag)
77 cout<<"No duplicates."<<endl;
78 else
79 {
80 sort(sort_out,sort_out+ps); //把待输出按字典序排序
81
82 for(int i=0;i<ps;i++)
83 {
84 cout<<setfill('0')<<setw(3)<<sort_out[i]/10000;
85 cout<<'-';
86 cout<<setfill('0')<<setw(4)<<sort_out[i]%10000;
87 cout<<' '<<time[ sort_out[i] ]<<endl;
88 }
89 }
90 }
91 return 0;
92 }

 

==============华丽的分割线===============

 1 //Memory Time
2 //644K 672MS
3
4 /*Qsort*/
5
6 #include<iostream>
7 #include<algorithm>
8 #include<iomanip>
9 using namespace std;
10
11 void initial(int* ctoi) //把字符ch转换为其在手机上对应的数字键
12 {
13 for(int i=0;i<=9;i++)
14 ctoi[i+'0']=i;
15
16 ctoi['A'] = ctoi['B'] = ctoi['C'] = 2;
17 ctoi['D'] = ctoi['E'] = ctoi['F'] = 3;
18 ctoi['G'] = ctoi['H'] = ctoi['I'] = 4;
19 ctoi['J'] = ctoi['K'] = ctoi['L'] = 5;
20 ctoi['M'] = ctoi['N'] = ctoi['O'] = 6;
21 ctoi['P'] = ctoi['R'] = ctoi['S'] = 7;
22 ctoi['T'] = ctoi['U'] = ctoi['V'] = 8;
23 ctoi['W'] = ctoi['X'] = ctoi['Y'] = 9;
24 return;
25 }
26
27 int main(int i)
28 {
29 int ctoi['Z'+1];
30 initial(ctoi);
31
32 int n; //号码数
33 while(cin>>n)
34 {
35 /*Initial*/
36
37 int* sort_out=new int[n]; //按字典序存放待输出的电话号码
38
39 /*Input*/
40
41 for(i=0;i<n;i++)
42 {
43 int x=0;
44 char s[20];
45 cin>>s;
46
47 for(int j=0;s[j]!='\0';j++)
48 {
49 if(s[j]=='-' || s[j]=='Q' || s[j]=='Z')
50 continue;
51 x=x*10+ctoi[ s[j] ];
52 }
53 sort_out[i]=x;
54 }
55
56 /*Sort & Output*/
57
58 sort(sort_out,sort_out+n);
59
60 bool flag=true; //标记是否所有号码都是唯一的
61 for(i=0;i<n;)
62 {
63 int time=0; //ort_out[i]出现的次数
64 int k=sort_out[i];
65 bool sign=false; //标记k出现次数是否大于2
66
67 while(k==sort_out[i] && i<n)
68 {
69 time++;
70 i++;
71
72 if(time==2)
73 {
74 flag=false;
75 sign=true;
76 }
77 }
78
79 if(sign)
80 {
81 cout<<setfill('0')<<setw(3)<<k/10000;
82 cout<<'-';
83 cout<<setfill('0')<<setw(4)<<k%10000;
84 cout<<' '<<time<<endl;
85 }
86 }
87 if(flag)
88 cout<<"No duplicates."<<endl;
89
90 delete sort_out;
91 }
92 return 0;
93 }

 

posted on 2011-07-30 21:38  小優YoU  阅读(1481)  评论(0编辑  收藏  举报

导航