-
Notifications
You must be signed in to change notification settings - Fork 0
/
296.c
50 lines (43 loc) · 931 Bytes
/
296.c
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
// http://www.bjfuacm.com/problem/296/
#include <stdio.h>
/**
* 5
* R W R B R
*
* R R R W B
*/
void swap(char *a, char *b) {
char temp = *a;
*a = *b;
*b = temp;
}
void arrange(char c[], int n) {
int i = 0;
int j = 0;
int k = n - 1;
while (j <= k) {
// algorithm: loop White, exchanging Red <- White -> Blue
if (c[j] == 'R') {
swap(c + i, c + j);
i++;
j++;
} else if (c[j] == 'W') {
j++;
} else { // 'B'
swap(c + j, c + k);
k--;
}
}
}
int main() {
int n;
char arr[200];
while (1) {
scanf("%d", &n);
if (n == 0) break;
for (int i = 0; i < n; i++) scanf("%s", &arr[i]);
// %c with input space, %s escape space
arrange(arr, n);
for (int i = 0; i < n; i++) printf("%c%s", arr[i], i == n - 1? "\n": " ");
}
}