北大ACM(POJ1002-487-3279)

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 }

 

 

转载于:https://www.cnblogs.com/TYcnblogs/p/poj1002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值