File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+ using ll = int64_t ;
5
+ using ff = long double ;
6
+
7
+ int diff (string const & S, string const & R) {
8
+ assert (S.size () == R.size ());
9
+ int count = 0 ;
10
+ for (int i = 0 ; i < (int )S.size (); ++i) {
11
+ count += S[i] != R[i] ? 1 : 0 ;
12
+ }
13
+ return count;
14
+ }
15
+
16
+ string solve (int const N, int const K, string const & S) {
17
+ string L = S;
18
+ for (int i = 0 ; i < N; ++i) {
19
+ int j = i;
20
+ for (int k = i + 1 ; k < N; ++k) {
21
+ if (L[k] >= L[j]) continue ;
22
+ string R = L;
23
+ swap (R[i], R[k]);
24
+ if (diff (S, R) <= K) {
25
+ j = k;
26
+ }
27
+ }
28
+ swap (L[i], L[j]);
29
+ }
30
+ return L;
31
+ }
32
+
33
+ int main () {
34
+ ios_base::sync_with_stdio (false );
35
+ cin.tie (0 ); cout.tie (0 );
36
+
37
+ int N, K;
38
+ string S;
39
+ cin >> N >> K >> S;
40
+ cout << solve (N, K, S) << endl;
41
+
42
+ return 0 ;
43
+ }
You can’t perform that action at this time.
0 commit comments