uva oj 10905 解题报告

 

 

快速排序||特殊情况分析


1 . 题目描述

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


 


2 .测试用例

input:
2
2212 221

5
9 99 999

output:
2212221
999999

注:这里要特别注意第一组测试用例,这种情况为这道题的特殊情况

3 .  算法描述

    1. 主体思路:

             将输入的数存入一个数组当中,根据题意对数组当中的元素进行快速排序(这里比较两个字符串大小的函数非常关键),然后输出排序后的数组,这个输出就是答案

    2. 算法实现细节

            1.这一节主要描述一下比较两个字符串s1与s2大小的函数,比较s1与s2的大小等价于比较s1+s2与s2+s1的字典序大小。所以算法首先按位比较s1与s2两个字符串,如果在这一步就能比较出两个字符串哪一个大则返回。
            2. 下面考虑一种特殊情况 s1 = n*s2+s3 、s2 = s2,可见这种情况需要对s2增添一些新的字符来与s1比较(因为s2全部比较完以后还没有分出大小),那么增加什么字符呢?应当按位增加s2就可以了(这里模拟了s2+s1所以添加的为两个字符串中重复的部分即s2),如果还需要对s1添加字符则依然按位添加s2即可(这种情况在s3=s2的一部分时出现,模拟了s1+s2这种情况,所以添加的依然为两个字符串当中重复的部分),在s1.length+s2.length内一定能分出大小
            3.利用2的方法,在(n+1)*s2.length()大小内一定能分出大小。下面证明下这个结论 a.s3与s2部分重叠 b.s1与s2添加新的填充字符s2 c.这种情况等价于将s1与s1错位s3.length()位比较,如果s2的各个位置不是全部一样,那么一定可以找打有差异的哪一位,并判断大小(s1 = 2212,s2=221,s3=2。按照之前的证明,其实最后的比较是s2错一位与自己比较,一定可以分出大小)       
                
 

 


4 .程序中遇到的问题

           相等的情况需要特别注意, s1=n*s2 ,s2 =s2 .这里需要有个计数器,当计数器的值等于s1.length+s2.length时两个数如果还相等,那么证明这两个串无论谁在前字典序是一样的。
           

5. 需要改进的地方

            java当中已经实现的排序,需要查一下,怎样调用,不用自己写了。

6.代码

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值