-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswexpert_1244.cpp
81 lines (68 loc) · 1.49 KB
/
swexpert_1244.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
78
79
80
81
#include<iostream>
using namespace std;
char str[7]; // 입력 string
char ans[7]; // 결과 string
int swapCount; // swap 횟수
int len; // 입력 string 길이
int strlen(char * str) {
int cnt = 0;
while (str[cnt++] != '\0')
;
return cnt - 1;
}
int strcmp(const char *s1, const char *s2) {
int ret = 0;
while (!(ret = *(unsigned char *)s1 - *(unsigned char *)s2) && *s2)
++s1, ++s2;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1;
return ret;
}
char *strcpy(char *dst, const char *src) {
char *cp = dst;
while (*cp++ = *src++)
;
return dst;
}
void swap(char *str, int i, int j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
void solve(char *str, int cnt, int offset) {
// 종료조건 : swap 선택이 완료된 경우
if (cnt == 0) {
// str이 더 큰 값일 경우 갱신
if (strcmp(str, ans) > 0) {
strcpy(ans, str);
}
return;
}
for (int i = offset; i < len; ++i) {
for (int j = i; j < len; ++j) {
if (i == j || str[i] > str[j]) continue; // i의 값이 클경우
swap(str, i, j);
solve(str, cnt - 1, i); // i위치부터 추가 진행
swap(str, j, i);
}
}
}
int main() {
int T;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++)
{
// data input
cin >> str;
cin >> swapCount;
// solve
len = strlen(str);
strcpy(ans, "");
solve(str, swapCount, 0);
// data output
cout << "#"<< test_case << " " << ans << endl;
}
return 0;
}