您的位置:首页 > 其它

UVa 10905 - Children's Game

2013-01-30 19:39 399 查看
4th
IIUC
Inter-University Programming Contest, 2005
A
Children’s Game
Input: standard input

Output: standard output


Problemsetter:
Md. Kamruzzaman


There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given
N positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made – 1231245690, 1241235690, 5612312490, 9012312456, 9056124123
etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made.
You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?
Input
Each input starts with a positive integer N (≤ 50). In next lines there are
N positive integers. Input is terminated by N = 0, which should not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending all the
N integers.
Sample Input
Output for Sample Input
4

123 124 56 90

5

123 124 56 90 9

5

9 9 9 9 9

0

9056124123

99056124123

99999


很久没有做UVa 了,做完这周的POJ计划后,突然想做一下UVa, 就看到了这题。 做这题的时候我已开始想要用int来开变量,但是后来我发现不如用字符串简单,于是乎就用的字符串,可是做完后老是RE,我就很奇怪,一个整数的位数能有多少啊,迫于无奈就把数组放到了100,但是我发现还是RE, 百般无奈之下我就把数组放到了1000,结果过了, 最后我测了一下,这题的整数的位数 最高能到80多位,幸亏没有用int,否则查多久也查不出来。
这就是UVa强大的后台数据啊。

至于思路我是借用了基数排序的思想,将第一个数统计, 如果第一个数字最大的唯一,那么这个数字肯定在前面,如果不唯一则将这些相同的数字两两比较选出最优的。 我在写这题的时候害怕因为这样两两比较超时,我就优化了一下,如果最高位相同的数字有n位,我把他的时间复杂度降到O(n)。

#include <stdio.h>
#include <string.h>
#include <math.h>
struct num
{
int pos,next;
}a[100];
int b[15];
char val1[1000],val2[1000],compare1[1000],compare2[1000];
int res[1000],status[1000];
char s1[100][100];
int main()
{
int i,j,n,m,s,t,tag,key,x,flag;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
memset(b,-1,sizeof(b));
memset(status,0,sizeof(status));
tag=0;
for(i=1;i<=n;i++)
{
scanf("%s",s1[i]);
a[tag].pos=i;
a[tag].next=b[s1[i][0]-'0'];
b[s1[i][0]-'0']=tag;
tag++;
}
for(i=1;i<=n;i++)
{
flag=0;
for(j=9;j>=0;j--)
{
if(b[j]!=-1)
{
for(x=b[j]; x!=-1; x=a[x].next)
{
if(!status[a[x].pos])
{
flag=1;
strcpy(val1,s1[a[x].pos]);
key=a[x].pos;
break;
}
}
for(x=a[x].next; x!=-1; x=a[x].next)
{
if(!status[a[x].pos])
{
strcpy(val2,s1[a[x].pos]);
strcpy(compare1,val1);
strcat(compare1,val2);
strcpy(compare2,val2);
strcat(compare2,val1);
if(strcmp(compare2,compare1)>0)
{
key=a[x].pos;
strcpy(val1,val2);
}
}
}
}
if(flag)
{
res[i]=key;
status[key]=1;
break;
}
}
}
for(i=1;i<=n;i++)
{
printf("%s",s1[res[i]]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: