-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdutch_nationalflag_problem_variant2.cpp
68 lines (58 loc) · 1.93 KB
/
dutch_nationalflag_problem_variant2.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
//https://ide.geeksforgeeks.org/cHBu6QMdQO
//dutch national flag problem - variant 2
#include<bits/stdc++.h>
using namespace std;
int mod=1e9+7;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
vector<char> Keys = {'A', 'B', 'C', 'D'};
void dutchNationalFlagV1(vector<char> A, int n){
int smallest = 0, current = 0, largest = n - 1;
int s1 = 0, s2 = 0;
while(current < largest){
// [A, A, A, [_, _, _, _,] D, D, D, D] //This sorts all the A's and D's
if(A[current] == Keys[0]){
s1 = smallest;
swap(A[smallest], A[current]);
smallest++; current ++;
}
else if(A[current] == Keys[1] || A[current] == Keys[2])
current++;
else{
s2 = largest;
swap(A[current], A[largest]);
largest--;
}
}
current = smallest = s1++;
largest = s2--;
while(current < largest){
// [A, A, A, [_, _, _, _,] D, D, D, D] //This sorts all the _, _, i.e. B and C's
if(A[current] == Keys[1]){
//s1 = smallest;
swap(A[smallest], A[current]);
smallest++; current ++;
}
else{
//s2 = largest;
swap(A[current], A[largest]);
largest--;
}
}
for(int i=0; i<n; i++)cout<<A[i]<<" ";
}
int main()
{
//code
FAST_INP;
int T;
cin>>T;
while(T--)
{
int n; cin>>n;
vector<char> A(n);
for(int i=0; i<n; i++)cin>>A[i];
dutchNationalFlagV1(A, n);
}
return 0;
}