題意:
給定n個字符串,只包含數字,用所有的字符串組成一個大的字符串,要求去掉前導零後字典序最小
思路:
經典貪心問題,
不是簡單的按照字典序排序,因爲兩個字符串長度不同會影響他們組合後的字典序,
對於兩個字符串s,t, 通過比較st 和 ts 的字典序,就能知道他們順序了
#include<bits/stdc++.h>
#include<cstring>
#define FI first
#define SE second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1e4 + 7;
const int INF = 0x7f7f7f7f;
int n;
struct node {
string s, t;
}a[maxn];
bool cmp(node a, node b) {
string s1 = a.s + b.s;
string s2 = b.s + a.s;
return s1 < s2;
}
string solve(string s) {
int t = 0, len = s.size();
while(s[t] == '0' && t < len) t++;
return s.substr(t, len-t);
}
int main() {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i].s;
a[i].t = solve(a[i].s);
}
sort(a+1, a+1+n, cmp);
bool ok = false;
for(int i = 1; i <= n; ++i) {
if(ok) {
cout << a[i].s;
}
else{
cout << a[i].t;
if(a[i].t.size() > 0) ok = true;
}
}
if(!ok) cout << 0;
return 0;
}