-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL030.cpp
65 lines (60 loc) · 1.26 KB
/
SCTDL030.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
/**
* @file SCTDL030.cpp
* @author long ([email protected])
* @brief swap k times each number in a specific number
* to create the largest possible number
* @version 0.1
* @date 2023-03-05
*
* @copyright Copyright (c) 2023
*
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
void solve(int k, std::string s)
{
std::vector<char> element;
for(int i=0; i < s.size(); i++)
{
element.push_back(s[i]);
}
std::sort(element.begin(), element.end());
for(int i=0; i < s.size(); i++)
{
if(k)
{
if(element.back() != s[i])
{
char tmp = s[i];
s[i] = element.back();
for(int j=s.size()-1; j > i; j--)
{
if(s[j] == element.back())
{
s[j] = tmp;
break;
}
}
element.pop_back();
k--;
} else {
element.pop_back();
}
}
}
std::cout << s << std::endl;
}
int main()
{
int t;
std::cin >> t;
while(t--)
{
int k;
std::string s;
std::cin >> k >> s;
solve(k, s);
}
}