1038. Recover the smallest number (30)
Given a collection of number segments, you is supposed to recover the smallest number from them. For example, given {321, 3214, 0229,}, 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 the one test case. Each case gives a positive an integer n (<=10000) followed by N number segments. Each segment contains a non-negative an integer of no more than 8 digits. All the numbers in a line is separated by a space.
Output Specification:
For each test case, print the smallest number on one line. Do not output leading zeros.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
1#include <iostream>2#include <string>3#include <vector>4#include <algorithm>5 6 using namespacestd;7 8 BOOLcmpConst string& S1,Const string&S2)9 {Ten returnS1 + s2 < s2 +S1; One } A - intMain () - { the intSegnum; -vector<string>segment; -CIN >>Segnum; - for(inti =0; i < Segnum; i++) + { - stringseg; +CIN >>seg; A segment.push_back (SEG); at } - sort (Segment.begin (), Segment.end (), CMP); - stringRes; - for(inti =0; I < segment.size (); i++) -Res + =Segment[i]; - in string:: Iterator it =Res.begin (); - while(*it = ='0') toit =Res.erase (it); + - if(Res.empty ()) thecout <<"0"; * Else $cout <<Res;Panax Notoginseng}
PAT 1038. Recover the smallest number (30)