-
Notifications
You must be signed in to change notification settings - Fork 46
/
PTIT_BCNEPER.cpp
59 lines (53 loc) · 1.4 KB
/
PTIT_BCNEPER.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
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*Ham doi cho*/
void Swap(char &a, char &b)
{
char temp = a;
a = b;
b = temp;
return;
}
/*Ham in ra hoan vi ke tiep*/
string Inhoanvi(string mang, int n)
{
string mangbandau = mang; // De so sanh tim hoan vi BIGGEST
int i = n - 2;
while(i>=0 && mang[i]>=mang[i+1]) i--;
if (i>=0)
{
int k = n-1; // mang[k] la phan tu cuoi day
while(mang[k] <= mang[i]) k--; // lui dan k de tim mang[k] dau tien lon hon mang[i]
Swap(mang[k],mang[i]); // Doi cho mang[k] va mang [i]
int a = i+1;
int b = n-1;
while(a<b)
{
Swap(mang[a],mang[b]);
a++;
b--;
}
}
if (mangbandau == mang) mang = "BIGGEST"; // Neu sau khi hoa doi mak "mang" khong thay doi thi no la lon nhat
return mang;
}
int main()
{
int T;
cin >> T; // T hoan vi
vector<string> vt;
vt.push_back(" "); // Bo gia tri vt[0], bat dau tinh tu vt[1] theo de bai.
for (int i=1; i<=T; i++)
{
int temp;
cin >> temp; // Doc du lieu thu tu bo test cua de bai -> Bo no di, khong can thiet
string str;
cin >> str;
vt.push_back(str);
}
for (int i=1; i<=T; i++)
cout << i << " " << Inhoanvi(vt[i],vt[i].length()) << endl;
return 0;
}