思路:贪心策略(贪心中的经典问题之一------拼数)
1.既然要拼数,则先把整数化成字符串来处理
2.要使拼得的数最大,则让n个数中字典序最大的在前(很容易想到这个策略,但是注意考虑存在反例qwq),例如反例:a=321,b=32,按照字典序(即a>b),所以应有a+b>b+a,然而实际却是‘32132’<'32321',即a+b<b+a
3.所以应该改变策略,还是假定ab前面和后面已经有序了,而且ab不影响前面和后面的结果
4.要使a排前面最优,就要使ab的字典序比ba的字典序大,即使a+b>b+a
5.按照3的规则进行排序,依次输出即可
代码:
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <iterator>
#include<math.h>
#define debug() puts("what the fuck")
#define ll long long
#define INF 0x3f3f3f3f //无穷大
const int maxn=8000009;
const double pi = acos(-1.0); //π的大小
using namespace std;
string a[30];
bool cmp(string a,string b)
{
return a+b>b+a;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
cout<<a[i];
cout<<endl;
return 0;
}