-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWA_1244.cpp
63 lines (48 loc) · 1.69 KB
/
SWA_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
# include "stdafx.h"
#include<iostream>
#include<string>
#include<string.h>
#include <windows.h>
using namespace std;
string n;
int k, ans;
int D[1000000];
void go(string m, int cnt){
int ret = stoi(m);
if (D[cnt] > ret){
return;
}
if (cnt == k) {
if (ret > ans) {
ans = stoi(m);
}
return;
}
D[cnt] = ret;
for (int i = 0; i < m.size(); i++) {
for (int j = i + 1; j < m.size(); j++) {
string tmp = m;
swap(tmp[i], tmp[j]);
go(tmp, cnt + 1);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
//freopen("input.txt", "r", stdin);
int tc;
cin >> tc;
for (int t = 1; t <= tc; t++)
{
memset(D, -1, sizeof(D));
n = "";
k = 0, ans = 0;
cin >> n >> k;
go(n, 0);
cout << "#" << t << " " << ans << "\n";
}
Sleep(50000);
return 0;
}