给定一个字符串类型的数组strs,请找到一种拼接顺序,使得将所有的字符串拼接起来组成的大字符串是所有可能性中字典顺序最大的,并输出这个大字符串。
第一行:N(1<N<=100),代表数组的长度第二行:N个字符串,作为数组的元素,空格分开,字符串长度<=10
字典序最大的大字符串
5 a ac ab def d
defdacaba
#include "bits/stdc++.h" using namespace std; typedef long long LL; typedef pair<int, int> PII; const int INF = 0x3f3f3f3f; string s[105]; bool cmp(string n, string m) { return n + m > m + n; } int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } sort (s, s + n, cmp); for (int i = 0; i < n; i++) { cout << s[i]; } puts(""); return 0; }
这个cmp太神奇了,想不到,解释不了了。