基本思路是将字符串转换成数字排序,很纠结的是一直WA,不知道为什么,后来到官网找测试数据,调了很久才发现问题,是由于存储字符串的数组开小了。在找测试数据的时候发现了外国选手参赛时的作品,不得不佩服其简洁。
My code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define UNCODE(a) (a>='A'?((a>'Q'?(a-'A'-1):(a-'A'))/3+2):a-'0')
//#define DEBUG
int uncode(char* s)
{
int i;
int result=0;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='-') continue;
result=result*10+UNCODE(s[i]);
}
return result;
}
int cmp(const void* a,const void* b)
{
return *(int*)a-*(int*)b;
}
int main()
{
#ifdef DEBUG
freopen("data.dat","r",stdin);
//freopen("out.dat","w",stdout);
#endif
int* store;
char s[50];
int time;
int n=0;
int i;
int start,value;
int dup=0;
scanf("%d",&time);
store=(int*)malloc(sizeof(int)*(time));
for(n=0;n<time;n++)
{
scanf("%s",s);
store[n]=uncode(s);
}
qsort(store,time,sizeof(store[0]),cmp);
value=store[0];
start=0;
for(n=0;n<time;n++)
{
if(store[n]!=value){
if((n-start)>1){
printf("%03d-%04d %d\n",store[start]/10000,store[start]%10000,n-start);
dup=1;
}
start=n;
value=store[n];
}
}
if((n-start)>1){
printf("%03d-%04d %d\n",store[start]/10000,store[start]%10000,n-start);
dup=1;
}
if(!dup)
{
printf("No duplicates.\n");
}
free(store);
return 1;
}
foreigner code:
/* Copyright Derek Kisman (ACM ICPC ECNA 1999) */
char map[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', 0};
char ph[100000][9];
int nph;
char buf[1000];
main() {
int i, j, k, x, y, z, n;
char ch;
memset( ph, 0, sizeof(ph) );
scanf( " %d", &nph );
for( i = 0; i < nph; i++ ) {
scanf( " %s", buf );
x = 0;
for( j = 0; buf[j]; j++ ) {
if( buf[j] == '-' ) continue;
if( buf[j] >= 'A' && buf[j] <= 'Z' ) buf[j] = map[buf[j]-'A'];
ph[i][x++] = buf[j];
if( x == 3 ) ph[i][x++] = '-';
}
}
qsort( ph, nph, 9, strcmp );
x = 1; z = 0;
for( i = 1; i < nph; i++ ) {
if( strcmp( ph[i-1], ph[i] ) ) {
if( x > 1 ) {
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
x = 1;
} else {
x++;
}
}
if( x > 1 ) {
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
if( !z ) printf( "No duplicates.\n" );
}