/
GirlsAndBoys.cpp
70 lines (57 loc) · 1.19 KB
/
GirlsAndBoys.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
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <numeric>
using namespace std;
class GirlsAndBoys {
public:
int sortThem(string row) {
int ret = 0;
int i, n = row.size();
int counter = 0;
string tmp = row;
while (1) {
int localc = 0;
for (i=0; i<n-1; i++)
if (tmp[i] == 'B' && tmp[i+1] == 'G') {
char c = tmp[i];
tmp [i] = tmp[i+1];
tmp[i+1] = c;
localc++;
}
if (localc == 0) break;
counter += localc;
}
ret = counter;
tmp = row;
counter = 0;
while (1) {
int localc = 0;
for (i=0; i<n-1; i++)
if (tmp[i] == 'G' && tmp[i+1] == 'B') {
char c = tmp[i];
tmp [i] = tmp[i+1];
tmp[i+1] = c;
localc++;
}
if (localc == 0) break;
counter += localc;
}
ret = min(ret, counter);
return ret;
}
};
// Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor