题目链接http://poj.org/problem?id=1002
输入n个字符串代表n个电话号码,字符串由数字,横线和大写字母组成,数字就代表数字本身,字母有对应的数字,对应关系已经给出,每个字符串都可以以得到一个七位数字的电话号码
也就是说每个字符串都必须是有七个数字或者大写字母(不会出现Z和Q),按字典序输出有重复的号码和重复的次数,如果一个重复的都没有的话输出No duplicates. 。
简单的模拟,可以排序或者hash,如果hash的话得用到map
注意0的出现
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define M 100100 6 int lsy[M]; 7 char g[11]={'0','1','2','3','4','5','6','7','8','9'}; 8 int sreach(char op){ 9 if (op=='A'||op=='B'||op=='C') return 2; 10 if (op=='D'||op=='E'||op=='F') return 3; 11 if (op=='G'||op=='H'||op=='I') return 4; 12 if (op=='J'||op=='K'||op=='L') return 5; 13 if (op=='M'||op=='N'||op=='O') return 6; 14 if (op=='P'||op=='R'||op=='S') return 7; 15 if (op=='T'||op=='U'||op=='V') return 8; 16 if (op=='W'||op=='X'||op=='Y') return 9; 17 } 18 int main() 19 { 20 int n,i,j; 21 char yj[1110],yxy[10]; 22 scanf("%d",&n); 23 memset(lsy,0,sizeof(lsy)); 24 for (i=0;i<n;i++){ 25 scanf("%s",yj); 26 int x=strlen(yj),y=1000000; 27 for (j=0;j<x;j++){ 28 if (yj[j]<='9'&&yj[j]>='0') 29 lsy[i]+=(yj[j]-'0')*y,y/=10; 30 if (yj[j]<='Z'&&yj[j]>='A') 31 lsy[i]+=sreach(yj[j])*y,y/=10; 32 } 33 } 34 sort(lsy,lsy+n); 35 int x=lsy[0],ans=1,flag=0,e=0; 36 for (i=1;i<n;i++){ 37 while (lsy[i]==x) 38 ans++,i++,flag=1; 39 if (ans>1) { 40 int y=7; 41 while (y--) yxy[y]=g[x%10],x/=10; 42 for (j=0;j<7;j++) { 43 if (j==3) printf("-"); 44 printf("%c",yxy[j]); 45 } 46 printf(" %d\n",ans); 47 } 48 x=lsy[i]; 49 ans=1; 50 } 51 if (!flag) printf("No duplicates.\n"); 52 return 0; 53 }
可能数据比较大,map字符串会超时,所以还是化成数字。
1 #include<map> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 char sreach(char op){ 8 if (op=='A'||op=='B'||op=='C') return '2'; 9 if (op=='D'||op=='E'||op=='F') return '3'; 10 if (op=='G'||op=='H'||op=='I') return '4'; 11 if (op=='J'||op=='K'||op=='L') return '5'; 12 if (op=='M'||op=='N'||op=='O') return '6'; 13 if (op=='P'||op=='R'||op=='S') return '7'; 14 if (op=='T'||op=='U'||op=='V') return '8'; 15 if (op=='W'||op=='X'||op=='Y') return '9'; 16 } 17 int main() 18 { 19 char yj[1111]; 20 map<int,int>que; 21 map<int,int>::iterator iter; 22 int n,i,lsy[10]; 23 scanf("%d",&n); 24 while (n--){ 25 scanf("%s",yj); 26 int m=strlen(yj),x=1000000,y=0; 27 for (i=0;i<m;i++){ 28 if (yj[i]<='9'&&yj[i]>='0') y+=(yj[i]-'0')*x,x/=10; 29 if (yj[i]<='Z'&&yj[i]>='A') y+=(sreach(yj[i])-'0')*x,x/=10; 30 } 31 que[y]+=1; 32 } 33 int flag=0; 34 for (iter=que.begin();iter!=que.end();iter++){ 35 //printf("****\n"); 36 if (iter->second>1){ 37 flag=1; 38 int x=iter->first,y=1; 39 while (x){ 40 lsy[y++]=x%10; 41 x/=10; 42 } 43 for (i=y;i<=7;i++) lsy[i]=0; 44 for (i=7;i>0;i--){ 45 if (i==4) printf("-"); 46 printf("%d",lsy[i]); 47 } 48 printf(" %d\n",iter->second); 49 } 50 } 51 if (!flag) printf("No duplicates.\n"); 52 return 0; 53 }