Given that the array of elements finds the maximum possible number that can be formed using the elements of the array.
e.g. 10 9
ans: 910
2 3 5 78
En: 78532
100 9
ans: 9100
I know this problem has a solution using a custom string comparator, but I don't understand how this works.
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; bool compare ( string a, string b ) { return atoi( (a+b).c_str() ) < atoi((b+a).c_str() ); } int main() { vector<string> vs; string s; while ( cin >> s ) { vs.push_back(s); } sort( vs.begin(), vs.end(), compare ); for ( int i = vs.size()-1; i >= 0; i-- ) { cout << vs[i]; } }
Can anyone suggest an algorithm to solve this problem? An explanation of the above comparator will be appreciated. Thanks you
source share