-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2504.rs
154 lines (147 loc) · 4.53 KB
/
P2504.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Author : quickn (quickn.ga)
Email : [email protected]
*/
use std::io::{self, BufWriter, Write};
use std::str;
/* https://github.com/EbTech/rust-algorithms */
/// Same API as Scanner but nearly twice as fast, using horribly unsafe dark arts
/// **REQUIRES** Rust 1.34 or higher
pub struct UnsafeScanner<R> {
reader: R,
buf_str: Vec<u8>,
buf_iter: str::SplitAsciiWhitespace<'static>,
}
impl<R: io::BufRead> UnsafeScanner<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buf_str: Vec::new(),
buf_iter: "".split_ascii_whitespace(),
}
}
/// This function should be marked unsafe, but noone has time for that in a
/// programming contest. Use at your own risk!
pub fn token<T: str::FromStr>(&mut self) -> T {
loop {
if let Some(token) = self.buf_iter.next() {
return token.parse().ok().expect("Failed parse");
}
self.buf_str.clear();
self.reader
.read_until(b'\n', &mut self.buf_str)
.expect("Failed read");
self.buf_iter = unsafe {
let slice = str::from_utf8_unchecked(&self.buf_str);
std::mem::transmute(slice.split_ascii_whitespace())
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Kind {
Two, // (
Three, // [
Value(u32),
}
fn main() {
let (stdin, stdout) = (io::stdin(), io::stdout());
let (mut scan, mut sout) = (
UnsafeScanner::new(stdin.lock()),
BufWriter::new(stdout.lock()),
);
let s: String = scan.token();
let mut stack: Vec<Kind> = Vec::new();
let mut is_failed = false;
for c in s.chars() {
match c {
'(' => stack.push(Kind::Two),
'[' => stack.push(Kind::Three),
')' => {
if stack.len() == 0 {
is_failed = true;
break;
}
match stack.pop().unwrap() {
Kind::Value(val) => {
if let Some(&res) = stack.last() {
if res != Kind::Two {
is_failed = true;
break;
} else {
stack.pop().unwrap();
}
} else {
is_failed = true;
break;
}
stack.push(Kind::Value(val << 1));
}
Kind::Two => {
stack.push(Kind::Value(2));
}
_ => {
is_failed = true;
break;
}
}
}
_ => {
if stack.len() == 0 {
is_failed = true;
break;
}
match stack.pop().unwrap() {
Kind::Value(val) => {
if let Some(&res) = stack.last() {
if res != Kind::Three {
is_failed = true;
break;
} else {
stack.pop().unwrap();
}
} else {
is_failed = true;
break;
}
stack.push(Kind::Value(val * 3));
}
Kind::Three => {
stack.push(Kind::Value(3));
}
_ => {
is_failed = true;
break;
}
}
}
}
let mut val = 0;
while let Some(&kind) = stack.last() {
match kind {
Kind::Value(res) => {
val += res;
stack.pop().unwrap();
}
_ => {
break;
}
}
}
if val != 0 {
stack.push(Kind::Value(val));
}
}
if is_failed || stack.len() != 1 {
writeln!(sout, "0").ok();
} else {
match stack.pop().unwrap() {
Kind::Value(res) => {
writeln!(sout, "{}", res).ok();
}
_ => {
writeln!(sout, "0").ok();
}
};
}
}