forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ml
20 lines (18 loc) · 715 Bytes
/
b.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(* https://codingcompetitions.withgoogle.com/codejam/round/0000000000201bee/0000000000201d17 *)
let reverse s =
Bytes.init (Bytes.length s) (fun i -> match (Bytes.get s i) with '-' -> '+' | _ -> '-')
let process () =
let rec loop s count =
try
let pos = Bytes.rindex s '-' in
let s' = Bytes.sub s 0 (pos + 1) in
let l = Bytes.length s' in
if l >= 2 && Bytes.sub s (l - 2) 2 = (Bytes.of_string "+-")
then let s'' = (reverse s') in s''.[l - 1] <- '-'; loop s'' (count + 1)
else loop (reverse s') (count + 1)
with _ -> count in
loop (Bytes.of_string (read_line ())) 0
let () =
for i = 1 to read_int () do
Printf.printf "Case #%d: %d\n" i (process ())
done