-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
76 lines (65 loc) · 1.02 KB
/
C.cpp
File metadata and controls
76 lines (65 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
输入格式
n (1≤n≤20),表示 n 个数。
接下来一行 n 个正整数,大小不超过 104。
输出格式
拼成的最长时间。
*/
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
//要考虑534 53和536 53这类特殊情况,需要进行递归比较,不能单纯根据长度比较。
//sort中cmp函数的深层原理。参考https://www.2cto.com/kf/201208/151993.html
bool cmp(string a, string b)
{
int l1 = a.length();
int l2 = b.length();
if (l1 == l2)
return a > b;
else if (l1 > l2)
{
int l = l2;
string c = a.substr(0, l);
if (b == c)
{
string d = a.substr(0, l);
return cmp(d, c);
}
else
{
if (c > b)
return true;
else
return false;
}
}
else
{
int l = l1;
string c = b.substr(0, l);
if (a == c)
{
string d = b.substr(l);
return cmp(c, d);
}
else
{
if (a > c)
return true;
else
return false;
}
}
}
int main()
{
int n;
string arr[20];
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
sort(arr, arr + n, cmp);
for (int i = 0; i < n; i++)
cout << arr[i];
}