Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }
, we can recover many numbers such like 32-321-3214-0229-87
or 0229-32-87-321-3214
with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87
.
Input Specification
Each input file contains one test case. Each case gives a positive integer N (≤10^4) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.
Sample Input
Sample Output
这道题以前就没做对,这次也没做对,记录一下。
两次读完题的反应完全一致:谁“小”把谁排前面。
- 若两个数位数相同,按数值比较
- 若两个数位数不同,位数少的数字补0至位数相同,按数值比较。
我的cmp是这么写的
Debug模式在运行时报错,Assert failed: Invalid comparor
. 根据cppreference,comparer
需满足
- 反自反.对于所有的
a
,comp(a,a)==false
- 反对称.若
comp(a,b)==true
,则comp(b,a)==false
- 传递 若
comp(a,b)==true
,comp(b,c)==true
,则comp(a,c)==true
.
我们的comparer
违背了反对称性。comp(9,10)===comp(10,9)
查到string
的<
操作符按照字典序进行比较。而我们辛辛苦苦的补0
,就是要按字典序比较。
事实证明字典序也不对。考虑32
和321
,按照字典序,32
<321
,但这样拼接的32321
却比32132
要大。似乎还要考虑字符串的首位和末位。
查了别人的代码,最好的解决办法是,把问题原样丢给C++
我居然还想了半天,整个人都不好了😶
|
|