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 |
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强大的后台数据啊。
至于思路我是借用了基数排序的思想,将第一个数统计, 如果第一个数字最大的唯一,那么这个数字肯定在前面,如果不唯一则将这些相同的数字两两比较选出最优的。 我在写这题的时候害怕因为这样两两比较超时,我就优化了一下,如果最高位相同的数字有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; }
相关文章推荐
- uva 10905 - Children's Game
- UVA10905- Children's Game
- UVA - 10905 Children's Game
- UVA - 10905 Children's Game
- Children's Game UVA - 10905
- UVa 10905 Children's Game / 贪心
- UVA 10905 - Children’s Game
- Uva 10905 Children's Game
- UVA 10905 Children's Game
- UVA 10905 Children's Game
- UVA 10905 Children's Game
- UVA - 10905 Children's Game
- UVA - 10905-Children's Game
- UVA - 10905 Children's Game
- 【STL】uva 10905 Children's Game
- UVA 10905 Children's Game
- Uva-10905-Children’s Game
- (intermediate) UVA 10905 - Children's Game
- Children's Game UVA - 10905
- UVa 10905: Children's Game