-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rs
66 lines (63 loc) · 1.83 KB
/
solution.rs
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
use std::collections::VecDeque;
use std::io::{stdin, BufRead};
#[derive(Debug)]
enum State {
Nope,
B,
BU,
}
impl AsRef<str> for State {
fn as_ref(&self) -> &str {
match self {
State::Nope => "",
State::B => "B",
State::BU => "BU",
}
}
}
fn main() {
let stdin = stdin();
let mut stdin = stdin.lock();
stdin.lines().for_each(|line| {
let mut stack = VecDeque::new();
let mut result = String::new();
for c in line.unwrap().bytes() {
match c {
b'B' => {
stack.push_back(State::B);
}
b'U' => {
if let Some(State::B) = stack.back() {
stack.pop_back();
stack.push_back(State::BU);
} else {
while let Some(v) = stack.pop_front() {
result.push_str(v.as_ref());
}
result.push(c.into());
}
}
b'G' => {
if let Some(State::BU) = stack.back() {
stack.pop_back();
} else {
while let Some(v) = stack.pop_front() {
result.push_str(v.as_ref());
}
result.push(c.into());
}
}
_ => {
while let Some(v) = stack.pop_front() {
result.push_str(v.as_ref());
}
result.push(c.into());
}
}
}
while let Some(v) = stack.pop_front() {
result.push_str(v.as_ref());
}
println!("{}", result);
})
}