写这道题是为了纪念一下本人在写字符串题型的时候遇到的问题,以及在看了一个大佬的题解之后对字符串的认识更加深刻
直接上代码
#include <iostream>
#include <algorithm>
#include <string>
//#define LL long long
using namespace std;
//typedef long long LL;
//const int N = 1e7 + 10;
string str[30];
int n;
/*bool cmp(string a, string b)
{
return a> b;// 加入有321 > 32, 则最终答案为32132,明显小于32321,所有不能这样子比较
}*/
bool cmp(string a, string b)
{
return a + b > b + a;//避开了上述问题,还是321, 32, 这样的比较方法则是32132<32321,不会出现错误判断
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> str[i];
sort(str + 1, str + n + 1, cmp);//利用了字符串相加的特性
for (int i = 1; i <= n; ++i)
cout << str[i];
cout << endl;
return 0;
}