洛谷P1012拼数(简单题排序技巧)

题目链接:https://www.luogu.org/problemnew/show/P1012

题目很简单,但需要注意的是不是简单的对所有字串排序,那是不对的结果。

如300,30

所以应该对a+b>b+a这样排序才对

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <cstdio>
 6 #include <cstring>
 7 using namespace std;
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 const int maxn=1e6+5;
11 int vis[maxn];
12 int arr[maxn];
13 string s[maxn];
14 bool cmp(string a,string b)
15 {
16     return a+b>b+a;
17 }
18 
19 int main()
20 {
21     ios::sync_with_stdio(false); cin.tie(0);
22     
23     int n;
24     cin>>n;
25     for(int i=1;i<=n;i++)
26     {
27         cin>>s[i];
28     }
29     sort(s+1,s+1+n,cmp);
30     for(int i=1;i<=n;i++) cout<<s[i];
31     cout<<endl;
32     
33     
34     return 0;
35 }

完。

原文地址:https://www.cnblogs.com/redblackk/p/9738808.html