-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10098.cpp
66 lines (49 loc) · 973 Bytes
/
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXN 30
char STR[MAXN];
int sort_function(const void *a, const void *b) {
return ( *(char *)a - *(char *) b);
}
void INTERCHANGE(int m,int n) {
char c;
c = STR[m];
STR[m] = STR[n];
STR[n] = c;
}
int main() {
int i,n ,c,m,p,N,M,kase;
scanf("%d",&kase);
while(kase--) {
scanf("%s",STR);
n = strlen(STR) - 1;
qsort((void *)STR,n+1,1,sort_function);
printf("%s\n",STR);
while(1) {
c = 0;
for(i = n; i>=1; i --) {
if(STR[i] >STR[i-1]) {
c = 1;
break;
}
}
if(c ==0) break;
m = i - 1;
for(i = n; STR[i] <=STR[m]; i--);
p = i;
INTERCHANGE(m,p);
N = n;
M = m+1;
while(N > M) {
INTERCHANGE(N,M);
N--;
M++;
}
for(int j = 0; STR[j];j ++) printf("%c",STR[j]);
printf("\n");
}
printf("\n");
}
return 0;
}