计蒜客 - T2144 拼数

计蒜客 - T2144 拼数

题解:把所有数字看成字符串,但是难道直接降序排就结束了嘛,不是的,我们来看一个反例:31 312 虽然312>31但是明显31312 > 31231,所以我们不能简单的排序,我们需要比较两个字符串x,y 如果x+y>y+x就将x排到前面,否则将y排到前面

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
vector<string> a;
int cmp(string x, string y)
{
    return x + y > y + x;
}
int main(void)
{
    Zeoy;
    int t = 1;
    // cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            string s;
            cin >> s;
            a.push_back(s);
        }
        sort(all(a), cmp);
        for (auto i : a)
            cout << i;
        cout << endl;
    }
    return 0;
}
posted @ 2023-01-06 22:52  Zeoy_kkk  阅读(21)  评论(0编辑  收藏  举报