零、原题链接
一、题目描述
二、测试用例
三、解题思路
- 基本思路:
编写比较函数 cmp ,然后使用快排函数进行排序; - 具体思路:
- 编写比较函数 cmp
- 使用 sort 函数进行排序
- 输出结果
四、参考代码
时间复杂度:
O
(
l
ˉ
×
n
l
o
g
n
)
\Omicron(\bar{l} \times nlog\; n)
O(lˉ×nlogn)【
l
ˉ
\bar{l}
lˉ 是字符串平均长度】
空间复杂度:
O
(
l
ˉ
×
n
)
\Omicron(\bar{l} \times n)
O(lˉ×n)
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool cmp(const string& x, const string& y) {
int n = min(x.length(), y.length());
for (int i = 0; i < n; i++) {
if (x[i] != y[i]) {
return x[i] < y[i];
}
}
return x.length() < y.length();
}
int main() {
int n;
cin >> n;
vector<string> strs(n);
for (int i = 0; i < n; i++) {
cin >> strs[i];
}
sort(strs.begin(), strs.end(), cmp);
for (int i = 0; i < n; i++) {
cout << strs[i] << endl;
}
}
// 64 位输出请用 printf("%lld")