-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10098.cpp
77 lines (61 loc) · 1.53 KB
/
10098.cpp
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
77
#include <cstdio>
#include <cstring>
using namespace std;
void sortC(char *arr, int len)
{
int i = 0;
int j = len - 1;
char tmpC, pivot = arr[len / 2];
if (len < 2) return;
while (true) {
while (arr[i] < pivot)
++i;
while (arr[j] > pivot)
--j;
if (i >= j) break;
tmpC = arr[i];
arr[i++] = arr[j];
arr[j--] = tmpC;
}
sortC(arr, i);
sortC(arr + i, len - i);
}
int main(void)
{
char arr[20], tmpC;
int len, n, i, j, min, times;
scanf("%d", ×);
while (times-- > 0) {
scanf("%s", arr);
len = strlen(arr);
sortC(arr, len);
printf("%s\n", arr);
while (true) {
i = len - 1;
while (i > 0 && arr[i] <= arr[i - 1])
--i;
if (i > 0) {
min = i;
i--;
while (min < len && arr[min] < arr[i])
++min;
if (min < len) {
for (j = min + 1; j < len; ++j)
if (arr[j] > arr[i] && arr[j] < arr[min]) min = j;
tmpC = arr[i];
arr[i] = arr[min];
arr[min] = tmpC;
i++;
sortC(arr + i, len - i);
}
else
sortC(arr + i, len - i);
printf("%s\n", arr);
}
else
break;
}
printf("\n");
}
return 0;
}