-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBiggerIsGreater.cpp
76 lines (62 loc) · 1.47 KB
/
BiggerIsGreater.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int getMin(vector<int> temp, int start, int key){
int ans = 1000;
for(int i = start; i < temp.size(); i++){
if(temp[i] > key){
if(ans != 1000){
if(temp[i] < temp[ans]){
ans = i;
}
}
else{
ans = i;
}
}
}
return ans;
}
void swap(int& a, int& b){
int t = a;
a = b;
b = t;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int test;
cin >> test;
while(test){
string s;
cin >> s;
int n = s.size(), count = 0, i;
vector<int> temp(n);
for(int i = 0; i < n; i++){
temp[i] = s[i];
}
for(i = n-1; i > 0; i--){
if(temp[i] > temp[i-1]){
int index = getMin(temp, i, temp[i-1]);
swap(temp[i-1], temp[index]);
count++;
break;
}
}
if(count){
sort(temp.begin()+i, temp.end());
for(int i = 0; i < n; i++){
char c = temp[i];
cout << c;
}
}
else{
cout << "no answer";
}
cout << endl;
test--;
}
return 0;
}