唉,出师不利啊,本来挺简单的一道题错了好多遍,看来以后在POJ上要小心啦
把字符串转化为数字,这样比较省时间啊,还有要记得没有重复的时候输出的No duplicates.
,附带几个测试数据哈:
2
---3333--3-3-3--
-3333333输出:
333-3333 24
0000000
0010001
0000000
0010001输出:
000-0000 2
001-0001 2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num[500008];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int range(char s)
{
if(s=='A'||s=='B'||s=='C')return 2;
else if(s=='D'||s=='E'||s=='F')return 3;
else if(s=='G'||s=='H'||s=='I')return 4;
else if(s=='J'||s=='K'||s=='L')return 5;
else if(s=='M'||s=='N'||s=='O')return 6;
else if(s=='P'||s=='R'||s=='S')return 7;
else if(s=='T'||s=='U'||s=='V')return 8;
else if(s=='Y'||s=='W'||s=='X')return 9;
return 0;
}
int main()
{
int k,ncase,i,n,flag,a;
char str[50];
scanf("%d",&ncase);
k=0;
while(ncase--)
{
n=0;
scanf("%s",str);
for(i=0;str[i];i++)
{
if(str[i]>='0'&&str[i]<='9')
n=n*10+str[i]-'0';
else if(str[i]>='A'&&str[i]<='Z')
n=n*10+range(str[i]);
}
num[k++]=n;
}
qsort(num,k,sizeof(num[0]),cmp);
flag=0;a=1;
for(i=1;i<k;i++)
{
if(num[i]==num[i-1])a++;
else{
if(a>1){
printf("d-d %d\n",num[i-1]/10000,num[i-1]000,a);
flag=1;
}
a=1;
}
}
if(a>1){
printf("d-d %d\n",num[i-1]/10000,num[i-1]000,a);
flag=1;
}
if(flag==0)printf("No duplicates. \n");
return 0;
}