单纯地对子字符串进行排序是不行的,因为这些字字符串最后是要组合在一起的,而直接比较大小会忽略掉组合后因长度不同而产生的问题。可以举一个例子,就是b和ba,明显b比ba小,但组合起来后,bba却比bab大。所以,对于子字符串s1和s2,此题的关键不在于s1和s2的大小关系,而在于s1+s2和s2+s1的大小关系。
Run Time: 0sec
Run Memory: 312KB
Code length: 483Bytes
SubmitTime: 2011-05-2510:24:22
// Problem#: 1198
// Submission#: 777646
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(string a,string b) { return a + b < b + a; }
int main()
{
int T, N;
string sub[ 8 ];
int i;
cin >> T;
while ( T-- ) {
cin >> N;
for ( i = 0; i < N; i++ )
cin >> sub[ i ];
sort( sub, sub + N, cmp );
for ( i = 0; i < N; i++ )
cout << sub[ i ];
cout << endl;
}
return 0;
}