let none x = x
let n = Scanf.bscanf Scanf.Scanning.stdin "%d\n" none

let split_on_char char string = Str.split (Str.regexp char) string

let a_list =
  let str_list =
    Scanf.bscanf Scanf.Scanning.stdin "%[^\n]" (split_on_char " ") in
  List.map int_of_string str_list

let is_even n =
  if n mod 2 = 0
    then true
    else false

let count_half lst =
  let rec hojo lst count =
    if (List.fold_right (&&) (List.map is_even lst) true)
      then (hojo (List.map (fun n -> n/2) lst) count+1)
      else count
  in hojo lst 0

let () = print_int (count_half a_list)


let f i = match i with
  (a, b, c) -> a + b + c

let char_to_int c1 c2 c3 = (int_of_string (String.make 1 c1),
                            int_of_string (String.make 1 c2),
                            int_of_string (String.make 1 c3))
let input = Scanf.bscanf Scanf.Scanning.stdin "%c%c%c" char_to_int

let () = print_int (f input)

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


let f s1 s2 s3 = s1 + s2 + s3

let () = let a = Scanf.bscanf Scanf.Scanning.stdin "%d%d%d" f in
  Printf.printf "%d" a


let f s1 s2 s3 = s1 + s2 + s3

let a = Scanf.bscanf Scanf.Scanning.stdin "%d%d%d" f

let () = Printf.printf "%d" a


open Batteries
let () =
  let n = Scanf.scanf "%d " (fun a -> a) in

  let rec digits base n res =
    if n = 0 then res
    else digits base (n/base) ((n mod base)::res)
  in

  Printf.printf "%d\n" @@
    List.fold_left (+) 0 (digits 10 n [])


let main () =
  let s = Scanf.scanf "%s" Batteries.identity in
  let i = BatString.fold_left (fun acc elm -> if BatChar.equal elm '1' then acc + 1 else acc) 0 s in
  Printf.printf "%d\n" i

let _ = main ()


let count target s =
  let rec f i tmp =
    if i = String.length s then tmp
    else if s.[i] = target then f (i + 1) (tmp + 1)
    else f (i + 1) tmp
  in
  f 0 0

let () = Scanf.scanf "%s" (fun s -> s)
         |> count '1' |> Printf.printf "%d\n"


let f x = if x = '1' then 1 else 0
let () = Scanf.scanf "%c%c%c" (fun a b c -> f a + f b + f c) |> Printf.printf "%d\n"


open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f
let rec for_iter_ a b f = if a >= b then () else f a; for_iter_ (a+1) b f

let list_of_string str =
  let len = String.length str in
  let rec f i = if i >= len then [] else str.[i] :: f (i+1) in
  f 0

let () = read_line ()
  |> list_of_string
  |> List.filter ((==) '1')
  |> List.length
  |> printf "%d\n"


Scanf.scanf "%s %s" (fun s t ->
    let n = String.length s in
    let tn = String.length t in

    let check p =
        let rec loop j =
            if j = tn then true else
                if s.[p + j] = t.[j] || s.[p + j] = '?' then loop (j + 1) else false
        in
        loop 0
    in

    let make p =
        String.init n (fun i ->
            if s.[i] <> '?' then s.[i] else
                if i < p || i >= p + tn then 'a' else t.[i - p]
        )
    in

    let rec loop i acc =
        if i < 0 then acc else
            let acc = if check i then min acc (make i) else acc in
            loop (i - 1) acc
    in
    let r = loop (n - tn) "{" in
    print_endline @@ if r = "{" then "UNRESTORABLE" else r
)

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


let s' = read_line ()
let t = read_line ()
let n, m = String.(length s', length t)
let _ = String.(for i = n - m downto 0 do let b = ref true in iteri (fun j c -> match s'.[i + j] with '?' -> () | c' -> if c' <> c then b := false) t;
  if !b then (print_endline @@ map (function '?' -> 'a' | c -> c) @@ (sub s' 0 i) ^ t ^ (sub s' (i + m) @@ n - i - m); exit 0) done); print_endline "UNRESTORABLE"


let s' = read_line ()
let t = read_line ()
let f = String.map (function '?' -> 'a' | c -> c)
let n, m, u = String.(length s', length t, ref "")
let _ = String.(for i = n - m downto 0 do let b = ref true in u := init m (fun j -> match s'.[i + j] with '?' -> t.[j] | c -> if c <> t.[j] then b := false; c);
  if !b then (print_endline @@ (f @@ sub s' 0 i) ^ !u ^ (f @@ sub s' (i + m) @@ n - i - m); exit 0) done); print_endline "UNRESTORABLE"


let s' = read_line ()
let t = read_line ()
let n, m = String.(length s', length t)
let f _ = print_endline "UNRESTORABLE"; exit 0
let _ = if n < m then f (); print_endline @@ String.init n (fun i -> if i < n - m then if s'.[i] = '?' then 'a' else s'.[i] else if s'.[i] = '?' then t.[i - n + m] else if s'.[i] <> t.[i - n + m] then f () else t.[i - n + m])


Scanf.scanf "%s %s" @@ fun s' t->
let reg = Str.global_replace (Str.regexp ".") "[\\0\\?]" t in
print_endline @@ try
  let i = Str.search_backward (Str.regexp reg) s' (String.length s') in
  Str.string_before s' i ^ t ^ Str.string_after s' (i+String.length t)
  |> String.map (function | '?' -> 'a' | c -> c)
with _ -> "UNRESTORABLE"


Scanf.scanf "%s %s" @@ fun s' t->
let reg = Str.global_replace (Str.regexp ".") "[\\0\\?]" t in
print_endline @@ try
  let i = Str.search_backward (Str.regexp reg) s' (String.length s') in
  let s' = Str.string_before s' i ^ t ^ Str.string_after s' (i+String.length t) in
  let s' = String.map (function | '?' -> 'a' | c -> c) s' in
  s'
with _ -> "UNRESTORABLE"


let s' = read_line () in
let t = read_line () in
let regexp =
  Array.init (String.length t) (fun i->Printf.sprintf "[%c\\?]" t.[i])
  |> Array.to_list
  |> String.concat ""
  |> Str.regexp
in
try
  let i = Str.search_backward regexp s' (String.length s') in
  let s' = Str.string_before s' i^t^Str.string_after s' (i+String.length t) in
  let s' = String.map(function '?' -> 'a' | c->c) s' in
  Printf.printf "%s\n" s'
with _ -> print_endline "UNRESTORABLE"


module SSet = Set.Make(String);;
Scanf.(Array.(scanf" %s %s"@@fun s t->
	let open Printf in
	let k,l = String.length s,String.length t in
	print_endline@@
		if k<l then "UNRESTORABLE"
		else try
			let st = ref SSet.empty in
			for i=0 to k-l do
				(* printf "[%d %d]\n" i (i+l-1); *)
				let f = ref true in
				for j=i to i+l-1 do
					(* printf "  %c  %c\n" s.[j] t.[j-i]; *)
					if s.[j]='?' || s.[j]=t.[j-i] then () else f:=false;
				done;
				if !f then st := SSet.add (
					init k (fun j ->
						if i<=j && j<=i+l-1 then t.[j-i]
						else if s.[j] = '?' then 'a'
						else s.[j]
					) |> map (String.make 1) |> Array.to_list |> String.concat ""
				) !st;
			done;
			!st |> SSet.min_elt
		with Not_found -> "UNRESTORABLE"
))


open String
let rec iter a b f = if a >= b then [] else let v = f a in v :: iter (a+1) b f
let check a b =
  iter 0 (length a) (fun i -> a.[i] = '?' || a.[i] = b.[i])
  |> List.fold_left (&&) true
let () =
  Scanf.scanf "%s %s" @@ fun s' t ->
  let s'_len, t_len = length s', length t in
  iter 0 (s'_len-t_len+1) (fun i ->
    if check (sub s' i t_len) t then
      sub s' 0 i ^ t ^ sub s' (i+t_len) (s'_len-i-t_len)
      |> map (function '?' -> 'a' | c -> c)
      |> fun s -> Some s
    else
      None)
  |> List.fold_left (fun r -> function None -> r | Some s -> s) "UNRESTORABLE"
  |> print_endline


let q2a s = String.map (fun c -> if c = '?' then 'a' else c) s

let f s' t =
  let lens = String.length s' in
  let lent = String.length t in
  let rec inner si ti =
    if ti >= lent then true
    else
      match (s'.[si], t.[ti]) with
      | a, b when a = b -> inner (si + 1) (ti + 1)
      | '?', _ -> inner (si + 1) (ti + 1)
      | _, _ -> false
  in
  let rec outer si =
    if si > lens - lent then "UNRESTORABLE"
    else if inner si 0
    then String.sub s' 0 si ^ t ^ String.sub s' (si + lent) (lens - si - lent)
    else outer (si + 1)
  in
  outer 0

let () = Scanf.scanf "%s\n%s" f |> q2a |> print_endline


let array_of_string s = Array.init (String.length s) (fun i -> s.[i])

let () =
  let s' = read_line () in
  let t = read_line () in
  let regexp =
    array_of_string t
    |> Array.to_list
    |> List.map (Printf.sprintf "[%c\?]")
    |> String.concat ""
    |> Str.regexp in
  try
    let i = Str.search_backward regexp s' (String.length s') in
    Printf.printf "%s%s%s\n"
      (String.map (function '?' -> 'a' | c -> c) (String.sub s' 0 i))
      t
      (String.map (function '?' -> 'a' | c -> c) (String.sub s' (i + String.length t) (String.length s' - i - String.length t)))
  with Not_found -> print_endline "UNRESTORABLE"


Scanf.sscanf (read_line ()) "%d %d" (fun h w ->
    let s = Array.init h (fun _ -> Bytes.of_string @@ read_line ()) in
    for y = 0 to h - 1 do
        for x = 0 to w - 1 do
            if Bytes.get s.(y) x = '.' then (
                let acc = 0 in
                let acc = if y > 0     && x > 0     && Bytes.get s.(y - 1) (x - 1) = '#' then acc + 1 else acc in
                let acc = if y > 0                  && Bytes.get s.(y - 1) (x)     = '#' then acc + 1 else acc in
                let acc = if y > 0     && x < w - 1 && Bytes.get s.(y - 1) (x + 1) = '#' then acc + 1 else acc in
                let acc = if              x > 0     && Bytes.get s.(y)     (x - 1) = '#' then acc + 1 else acc in
                let acc = if              x < w - 1 && Bytes.get s.(y)     (x + 1) = '#' then acc + 1 else acc in
                let acc = if y < h - 1 && x > 0     && Bytes.get s.(y + 1) (x - 1) = '#' then acc + 1 else acc in
                let acc = if y < h - 1              && Bytes.get s.(y + 1) (x)     = '#' then acc + 1 else acc in
                let acc = if y < h - 1 && x < w - 1 && Bytes.get s.(y + 1) (x + 1) = '#' then acc + 1 else acc in
                Bytes.set s.(y) x (char_of_int (acc + 48))
            )
        done
    done;
    for y = 0 to h - 1 do
        print_endline @@ Bytes.to_string s.(y)
    done
)

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let f i j = List.fold_left (fun c (dy, dx) -> let y, x = i + dy, j + dx in if 0 <= y && y < h && 0 <= x && x < w && ss.(y).[x] = '#' then c + 1 else c) 0 [-1, 0; -1, -1; 0, -1; 1, -1; 1, 0; 1, 1; 0, 1; -1, 1]
let _ = for i = 0 to h - 1 do for j = 0 to w - 1 do if ss.(i).[j] = '#' then print_char '#' else print_int @@ f i j done; print_newline () done


let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let _ = for i = 0 to h - 1 do for j = 0 to w - 1 do if ss.(i).[j] = '#' then print_char '#' else print_int @@ List.fold_left (fun c (dy, dx) ->
  let y, x = i + dy, j + dx in if 0 <= y && y < h && 0 <= x && x < w && ss.(y).[x] = '#' then c + 1 else c) 0 [-1, 0; -1, -1; 0, -1; 1, -1; 1, 0; 1, 1; 0, 1; -1, 1] done; print_newline () done


let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let _ =
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do
      let f c (dy, dx) =
        let y, x = i + dy, j + dx in if 0 <= y && y < h && 0 <= x && x < w && ss.(y).[x] = '#' then c + 1 else c in
      if ss.(i).[j] = '.' then print_int @@ List.fold_left f 0 [-1, 0; -1, -1; 0, -1; 1, -1; 1, 0; 1, 1; 0, 1; -1, 1]
      else print_char ss.(i).[j] done; print_newline () done


(* O(h w) *)
Scanf.scanf " %d %d" @@ fun h w ->
  let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s in
  let f a b =
    let c = ref 0 in
    for y = -1 to 1 do
      for x = -1 to 1 do
        let a_y, b_x = a + y, b + x in
        if 0 <= a_y && a_y < h && 0 <= b_x && b_x < w then
          if ss.(a_y).[b_x] = '#' then incr c
      done
    done;
    !c in
  for y = 0 to h - 1 do
    for x = 0 to w - 1 do
      if ss.(y).[x] = '.' then print_int @@ f y x else print_char '#'
    done;
    print_newline ()
  done


(* O(h w) *)
Scanf.scanf " %d %d" @@ fun h w ->
  let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s in
  let bs = Array.init h @@ fun _ -> Bytes.create w in
  let f a b =
    let c = ref 0 in
    for y = -1 to 1 do
      for x = -1 to 1 do
        let a_y, b_x = a + y, b + x in
        if 0 <= a_y && a_y < h && 0 <= b_x && b_x < w then
          if ss.(a_y).[b_x] = '#' then incr c
      done
    done;
    char_of_int @@ 48 + !c in
  for y = 0 to h - 1 do
    for x = 0 to w - 1 do
      Bytes.set bs.(y) x @@ if ss.(y).[x] = '.' then f y x else '#'
    done
  done;
  Array.iter (fun b -> print_bytes b; print_newline ()) bs


open Batteries
let () =
  let h,w = Scanf.scanf "%d %d " (fun a b -> a,b) in

  let mat = Array.init h (fun _ ->
      Array.init w (fun _ -> Scanf.scanf "%c " (fun a -> if a = '.' then '0' else a))
    ) in

  for i=0 to h-1 do
    for j=0 to w-1 do
      if mat.(i).(j) = '#' then (
        for i2=i-1 to i+1 do
          for j2=j-1 to j+1 do
            try
              if mat.(i2).(j2) <> '#' then
                mat.(i2).(j2) <- char_of_int @@ (int_of_char  mat.(i2).(j2)) + 1
            with _ -> ()

          done;
        done;
      ) else ()
    done;
  done;

  Array.iter (fun arr -> Array.iter (print_char) arr; print_endline "") mat;


let h, w = Scanf.scanf "%d %d\n" (fun a b -> a, b)
let ar = Array.init h (fun _ -> Scanf.scanf "%s\n" (fun x -> x))
let b = Buffer.create (h * w)
let d = [0,1;1,1;1,0;1,-1;0,-1;-1,-1;-1,0;-1,1]

let search pos =
  List.fold_left (fun acc (x, y) ->
      let ni, nj = fst pos + y, snd pos + x in
      if (match ni, nj with
          | a, _ when a < 0 || h <= a -> false
          | _, b when b < 0 || w <= b -> false
          | _, _ -> true)
      then
        (if ar.(ni).[nj] = '#' then acc + 1 else acc)
      else acc
    )  0 d

let () =
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do
      if ar.(i).[j] = '#' then Buffer.add_char b '#'
      else Buffer.add_string b (search (i,j) |> string_of_int)
    done ;
    Buffer.add_string b "\n"
  done ;
  Buffer.contents b |> print_string


open Printf
open Scanf

let () =
  let h, w = scanf "%d %d " (fun x y -> (x, y)) in
  let ar = Array.make_matrix h w 0 in
  let rec read i =
    if i = h then () else
      let s = scanf "%s " (fun x -> x) in
      String.iteri (fun j c -> if c = '#' then ar.(i).(j) <- (-1) else ()) s;
      read (i + 1) in
  read 0;
  let idx i j = [(i+1, j);(i-1, j);(i, j+1);(i, j-1);(i-1, j-1);(i-1, j+1);(i+1, j-1);(i+1, j+1)] |>
                   List.filter (fun (x, y) -> x >= 0 && x < h && y >= 0 && y < w) in
  Array.iteri (fun i a ->
               Array.iteri (fun j v ->
                            if v = (-1) then
                              List.iter (fun (x, y) ->
                                         if ar.(x).(y) >= 0 then
                                           ar.(x).(y) <- ar.(x).(y) + 1 else ()) (idx i j) else ()) a) ar;
  Array.iter (fun a -> Array.iter (fun v -> if v = (-1) then printf "#" else printf "%d" v) a; printf "\n") ar


Scanf.scanf "%d" (fun n ->
    let rec loop i acc =
        if i = n then acc else
            let acc = Scanf.scanf " %d %d" (fun l r -> acc + r - l + 1) in
            loop (i + 1) acc
    in
    loop 0 0 |> Printf.printf "%d\n"
)

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


(* Vicfred
 * https://atcoder.jp/contests/abc073/tasks/abc073_b
 * implementation
 * *)
let n = Scanf.scanf "%d\n" ( + ) 0
let lines = Array.init n @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun l r -> r - l + 1
let main = Array.fold_left ( + ) 0 lines |> Printf.printf "%d\n"


let n = Scanf.scanf " %d" (+) 0
let cs = Array.init n @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b -> b - a + 1
let _ = Printf.printf "%d\n" @@ Array.fold_left (+) 0 cs


open Printf
open Scanf

let () =
  let n = scanf "%d " (fun x -> x) in
  let rec loop i sm =
    if i = 0 then sm
    else let l, r = scanf "%d %d " (fun x y -> (x, y)) in loop (i-1) (sm + r - l + 1) in
  loop n 0 |> printf "%d\n"


let id x = x
let n = Scanf.scanf "%d\n" id
let array = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> (a,b)))
let list = Array.to_list array

let rec func l =
  match l with
  | [] -> 0
  | (a,b) :: rest -> (b-a+1) + func rest

let ans = func list

let () =
  Printf.printf "%d\n" ans


let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let lrs = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun l r -> l, r)) in
  Array.fold_left (fun a (l, r) -> a + r - l + 1) 0 lrs
  |> Printf.printf "%d\n"


(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries

let () =
        (
                let set = Str.split (Str.regexp "") (read_line ())
                          |> List.sort_uniq compare
                          |> Set.of_list
                in

                let abclst = ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j"; "k"; "l"; "m"; "n"; "o"; "p"; "q"; "r"; "s"; "t"; "u"; "v"; "w"; "x"; "y"; "z"]
                             |> Set.of_list
                in
                        try
                                Set.diff abclst set
                                |> Set.min_elt
                        with Not_found -> ("None")
        ) |> Printf.printf "%s\n"

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


open Batteries
let explode s = List.init (String.length s) (String.get s)
let s = (explode (read_line ()))

let all = explode "qwertyuiopasdfghjklzxcvbnm"
let rec ans lst =
  match lst with
   [] -> []
  | first :: rest -> if List.exists (fun p -> p = first) s then ans rest else first :: ans rest

let a = List.sort compare (ans all)
let _ =
  match a with
  | [] -> print_endline "None"
  | first :: rest -> print_char first; print_string "\n"


open Batteries
let explode s = List.init (String.length s) (String.get s)
let s = (explode (read_line ()))

let all = explode "qwertyuiopasdfghjklzxcvbnm"
let rec ans lst =
  match lst with
   [] -> ' '
  | first :: rest -> if List.exists (fun p -> p = first) s then ans rest else first

let a = ans all
let _ =
  match a with
  | ' ' -> print_endline "None"
  | _ -> print_char a; print_string "\n"


open Batteries
let explode s = List.init (String.length s) (String.get s)
let s = (explode (read_line ()))

let all = explode "qwertyuiopasdfghjklzxcvbnm"
let rec ans lst =
  match lst with
   [] -> ' '
  | first :: rest -> if List.exists (fun p -> p = first) s then ans rest else first

let a = ans all
let _ =
  match a with
  | ' ' -> print_endline "None"
  | _ -> print_char a


let s = read_line ()
let _ = Char.(for i = code 'a' to code 'z' do if not @@ String.contains s @@ chr i then (Printf.printf "%c\n" @@ chr i; exit 0) done); print_endline "None"


let s = read_line ()
let _ = for i = Char.code 'a' to Char.code 'z' do if not @@ String.contains s @@ Char.chr i then (Printf.printf "%c\n" @@ Char.chr i; exit 0) done;
  print_endline "None"


(* O(|s|) *)
let s = read_line () in
for i = int_of_char 'a' to int_of_char 'z' do
  let c = char_of_int i in
  try String.index s c |> ignore with
  | _ -> Printf.printf "%c\n" c; exit 0
done;
print_endline "None"


(* O(|s|) *)
let s = read_line () in
let flags = Array.make 128 false in
String.iter (fun c -> flags.(int_of_char c) <- true) s;
for i = int_of_char 'a' to int_of_char 'z' do
  if not flags.(i) then (Printf.printf "%c" @@ char_of_int i; exit 0)
done;
print_endline "None"


open Batteries

let ( ++ ) x y = int_of_char x + y |> char_of_int

let () =
  Scanf.scanf "%s\n" String.to_list
  |> List.sort_uniq compare
  |> List.fold_left (fun x y -> if x = y then x ++ 1 else x) 'a'
  |> (fun x -> if x > 'z' then "None" else String.of_char x)
  |> print_endline


let () =
  let s = read_line () in
  try
    Array.init 26 (fun i -> Char.chr @@ Char.code 'a' + i)
    |> Array.to_list
    |> List.find (fun c ->
        Array.fold_left ( && ) true @@
        Array.init (String.length s) (fun i -> s.[i] <> c))
    |> Printf.printf "%c\n"
  with Not_found -> print_endline "None"


let mkls s =
  let len = String.length s in
  let rec f i ls =
    if i < 0 then List.sort_uniq Char.compare ls
    else f (i - 1) (s.[i] :: ls)
  in
  f (len - 1) []

let solve ls =
  let i2s c = Char.chr c |> Char.escaped in
  if List.length ls = 26 then "None"
  else
    let rec f c = function
      | [] -> i2s c
      | h :: t when Char.chr c <> h -> i2s c
      | _ :: t -> f (c + 1) t
    in
    f (Char.code 'a') ls

let () = mkls (read_line ()) |> solve |> print_endline


open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f
let rec for_iter_ a b f = if a >= b then () else (f a; for_iter_ (a+1) b f)

let list_of_string s = range 0 (String.length s) |> List.map (fun i -> s.[i])

let lowers = list_of_string "abcdefghijklmnopqrstuvwxyz"

let () =
  let s = read_line () |> list_of_string in
  match List.filter (fun c -> List.filter ((==) c) s = []) lowers with
    | [] -> printf "None\n"
    | (x::_) -> printf "%c\n" x


open Printf
open Scanf

let () =
        let s = read_line() in
        let alpha = "abcdefghijklmnopqrstuvwxyz" in
        let rec iter i =
                (if (i = (String.length alpha)) then
                        "None"
                else if (String.contains s alpha.[i]) then
                        (iter (i + 1))
                else (String.make 1 alpha.[i])
                ) in
        printf "%s\n" (iter 0)


open Printf
open Scanf

let () =
  let s = scanf "%s\n" (fun x -> x) in
  let cs = "abcdefghijklmnopqrstuvwxyz" in
  let rec iter i =
    if i = 26 then "None"
    else
      if String.contains s cs.[i] then iter (i+1)
      else String.make 1 cs.[i]
  in
  printf "%s\n" (iter 0)


let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let as_ = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in
  let n_odd = List.length @@ List.filter (fun n -> n mod 2 = 1) as_ in
  let n_4n2 = List.length @@ List.filter (fun n -> n mod 2 = 0 && n mod 4 <> 0) as_ in
  let n_4n = List.length @@ List.filter (fun n -> n mod 4 = 0) as_ in
  print_endline @@ if n_odd <= n_4n || n_4n2 = 0 && n_odd <= n_4n + 1 then "Yes" else "No"

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


open Str

let read_list f =
  split (regexp " +") (read_line ()) |> List.map f

let () =
  let _ = read_line () in
  let ls = read_list int_of_string in
  let (o, e, f) = List.fold_left (fun (o', e', f') x ->
                                  if x mod 4 = 0 then (o', e', f'+1)
                                  else if x mod 2 = 0 then (o', e'+1, f')
                                  else (o'+1, e', f')) (0,0,0) ls in
  (if e = 0 then
     if o - 1 <= f then "Yes" else "No"
   else
     if o <= f then "Yes" else "No") |> print_endline


Scanf.scanf "%d %d %d" (fun n a b ->
    let mi = (n - 1) * a + b in
    let mx = (n - 1) * b + a in
    Printf.printf "%d\n" (max 0 (mx - mi + 1))
)

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


Printf.printf "%d\n" @@ Scanf.scanf "%d %d %d" @@ fun n a b ->
  if a > b || n = 1 && a <> b then 0 else (b*(n-1)+a) - (a*(n-1)+b) + 1


let id x = x
let (n,a,b) = Scanf.scanf "%d %d %d\n" (fun n a b -> (n,a,b))

let ans = if b-a < 0 || (b-a > 0 && n=1) then 0
    else if a=b then 1
    else (b-a) * (n-2) +1

let () =
  Printf.printf "%d\n" ans


let () = Scanf.scanf "%d %d %d" (fun n a b ->
  Printf.printf "%d\n" @@
    if b < a || n = 1 && a < b then 0
    else 1 + (b - a) * (n - 2))


let () =
  let s = read_line () in
  Array.init (String.length s) (fun i -> s.[i])
  |> Array.mapi (fun i -> function
    | 'U' -> 2 * i + String.length s - 1 - i
    | 'D' -> i + (String.length s - 1 - i) * 2)
  |> Array.fold_left ( + ) 0
  |> Printf.printf "%d\n"


Scanf.scanf "%s" (fun s ->
    let n = String.length s in
    let rec loop i mi mx =
        if i = n then mx - mi + 1 else
            if s.[i] = 'Z' then loop (i + 1) mi i else
            if s.[i] = 'A' then loop (i + 1) (min mi i) mx else
                loop (i + 1) mi mx
    in
    loop 0 max_int 0 |> Printf.printf "%d\n"
)

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


let s = read_line ()

let rec f i =
  if s.[i] = 'A' then i
  else f (i + 1)

let rec g i =
  if s.[i] = 'Z' then i
  else g (i - 1)

let () = print_int (g (String.length s - 1) - f 0 + 1)


let s = read_line ()
let _ = String.(rindex s 'Z' - index s 'A' + 1) |> Printf.printf "%d\n"


let s = read_line ()
let _ = Printf.printf "%d\n" @@ String.rindex s 'Z' - String.index s 'A' + 1


(* O(|s|) *)
let solve s = String.rindex s 'Z' - String.index s 'A' + 1

let _ = read_line () |> solve |> string_of_int |> print_endline


(* O(|s|) *)
let string_to_char_array s = Array.init (String.length s) (fun i -> s.[i])

(* O(|s|) *)
let solve s =
  let cs1 = string_to_char_array s |> Array.to_list |> List.mapi (fun i c -> i, c) in
  let cs2 = List.rev cs1 in
  let start = List.find (fun (_, c) -> c = 'A') cs1 |> fst in
  let stop = List.find (fun (_, c) -> c = 'Z') cs2 |> fst in
  stop - start + 1

let _ = read_line () |> solve |> string_of_int |> print_endline


let () =
  Scanf.scanf "%s" (fun s ->
      let st = String.index s 'A' in
      let e = String.rindex s 'Z' in
      Printf.printf "%d\n" (e - st + 1) )


let () =
  let s = read_line () in
  let head =
    Array.init (String.length s) (fun i -> i)
    |> Array.to_list
    |> List.filter (fun i -> s.[i] = 'A')
    |> List.hd in
  let tail =
    Array.init (String.length s) (fun i -> i)
    |> Array.to_list
    |> List.filter (fun i -> s.[i] = 'Z')
    |> List.rev
    |> List.hd in
  Printf.printf "%d\n" @@ tail - head + 1


open Batteries
let () =
  let s = Scanf.scanf "%s " (fun a -> a) in
  let rev_s = String.rev s in
  Printf.printf "%d\n" @@
  (String.length rev_s) - (String.find rev_s "Z") - (String.find s "A")


let () =
  let s = read_line () in
  Printf.printf "%d\n" (String.rindex s 'Z' - String.index s 'A' + 1)


let s = read_line ()
let () =
  (String.rindex s 'Z') - (String.index s 'A') + 1
  |> Printf.printf "%d\n"


open String
let () = Scanf.scanf " %s" (fun s -> rindex s 'Z' - index s 'A' + 1) |> Printf.printf "%d\n"


let s = read_line ();;
let slen = String.length s;;

let rec loop1 i en =
    if i == slen
    then
        en
    else
        match s.[i] with
            |'Z' -> loop1 (i+1) i
            |_ -> loop1 (i+1) en;;

let rec loop2 i =
    match s.[i] with
    |'A' -> i,loop1 i 0
    |_ -> loop2 (i+1);;

let (st,en) = loop2 0
    in
        Printf.printf "%d\n" (en - st+1)


let s = read_line ();;
let slen = String.length s;;

let rec loop1 i en =
    if i == slen
    then
        en
    else
        match s.[i] with
            |'Z' -> loop1 (i+1) i
            |_ -> loop1 (i+1) en;;

let rec loop2 i =
    match s.[i] with
    |'A' -> i,loop1 i 0
    |_ -> loop2 (i+1);;

let (st,en) = loop2 0
    in
        Printf.printf "%s\n" (String.sub s st (en-st+1)) ;;


let solve s =
  let l = String.index s 'A' in
  let r = String.rindex s 'Z' in
  r - l + 1

let () = read_line () |> solve |> string_of_int |> print_endline


let id x = x
let str = Scanf.scanf "%s\n" id
let r = String.length str

let indexa = String.index str 'A'
let indexz = String.rindex str 'Z'

let ans = indexz - indexa + 1

let () = Printf.printf "%d\n" ans


let () =
	let s = read_line () in
	print_int (String.rindex s 'Z' - String.index s 'A' + 1);
	print_newline ()


let k, s = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ans = ref 0
let _ = for x = 0 to k do for y = 0 to k do if 0 <= s - x - y && s - x - y <= k then incr ans done done; Printf.printf "%d\n" !ans

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


let k, s = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ans = ref 0
let _ =
  for x = 0 to k do
    for y = 0 to k do if 0 <= s - x - y && s - x - y <= k then incr ans done done;
  Printf.printf "%d\n" !ans


(* O(k^2) *)
Scanf.scanf "%d %d" @@ fun k s ->
  let ans = ref 0 in
  for x = 0 to k do
    for y = 0 to k do
      let z = s - x - y in
      if 0 <= z && z <= k then incr ans
    done
  done;
  Printf.printf "%d\n" !ans


let _ =
  let k, s = Scanf.scanf "%d %d\n" (fun k s -> k, s) in
  let res = ref 0 in
  for x=0 to k do
    for y=0 to k do
      if (s - x - y) >= 0 && k >= (s - x - y) then res := !res + 1;
    done
  done;
  !res |> print_int


let count = ref 0

let loop k s =
  for x = 0 to k do
    for y = 0 to k do
      let z = s - x - y in
      if 0 <= z && z <= k then incr count
    done
  done

let () =
  Scanf.scanf "%d %d" loop ;
  Printf.printf "%d\n" !count


let () = Scanf.scanf "%d %d" @@ fun k s ->
  Printf.printf "%d\n" @@
  if 3 * k < s
  then 0
  else
    Array.fold_left ( + ) 0 @@
    Array.init (1 + min s k) @@ fun x ->
      Array.fold_left ( + ) 0 @@ Array.init (1 + min (s - x) k) @@ fun y ->
        if s - x - y <= k then 1 else 0


let ans = ref 0
let k, s = Scanf.scanf "%d %d" (fun a b -> a, b)

let () =
  for x = 0 to k do
    for y = 0 to k do
      let z = s - x - y in
      if 0 <= z && z <= k then incr ans
    done;
  done;
  Printf.printf "%d\n" !ans


let k, s = Scanf.scanf "%d %d" (fun k s -> k, s)
let ans = ref 0

let () =
  for x = 0 to k do
    for y = 0 to k do
      if (s - x - y >= 0 && s - x - y <= k) then incr ans
    done ;
  done ;
  Printf.printf "%d\n" !ans


let () = Scanf.scanf "%d %d" (fun k s ->
  let arr = Array.init (k+1) (fun i -> i) in
  Array.fold_left (fun a i ->
    let b = Array.fold_left (fun a j -> if 0 <= s-i-j && s-i-j <= k then a+1 else a) 0 arr
    in a+b) 0 arr) |> Printf.printf "%d\n"


open Printf
open Scanf

let () =
  let k, s = scanf "%d %d\n" (fun x y -> (x, y)) in
  let mx = min k s in
  let count t = min (max 0 (2*k-t+1)) (t+1) in
  let rec iter x a =
    if x > mx then a
    else
      iter (x+1) (a + count (s-x)) in
  iter 0 0 |> printf "%d\n"


Scanf.scanf "%s" (fun s ->
    let n = String.length s in

    let rec loop i =
        if i = n then "YES" else
            if n - i >= 7 && String.sub s i 7 = "dreamer" then (
                if n - i = 7 then "YES" else
                    if s.[i + 7] = 'a' then loop (i + 5) else loop (i + 7)
            ) else
            if n - i >= 6 && String.sub s i 6 = "eraser"  then loop (i + 6) else
            if n - i >= 5 && String.sub s i 5 = "dream"   then loop (i + 5) else
            if n - i >= 5 && String.sub s i 5 = "erase"   then loop (i + 5) else
                "NO"
    in
    loop 0 |> print_endline
)

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


let asReversedString s =
  String.of_seq (List.to_seq (List.rev (List.of_seq (String.to_seq s))))

let isPrefix prefixString targetString =
  let prefixLength = String.length prefixString in
  let targetSub = String.sub targetString 0 prefixLength in
  prefixString = targetSub

let remove_prefix prefixString targetString =
  let prefixLength = String.length prefixString in
  let targetLength = String.length targetString in
  let newTargetStart = prefixLength in
  let newTargetLength = targetLength - prefixLength in
  String.sub targetString newTargetStart newTargetLength

let satisfy s =
  let reversed = asReversedString s in
  let word1 = asReversedString "dream" in
  let word2 = asReversedString "dreamer" in
  let word3 = asReversedString "erase" in
  let word4 = asReversedString "eraser" in
  let rec satisfy_aux s =
    if s = "" then true else
      if isPrefix word1 s then satisfy_aux (remove_prefix word1 s) else
        if isPrefix word2 s then satisfy_aux (remove_prefix word2 s) else
          if isPrefix word3 s then satisfy_aux (remove_prefix word3 s) else
            if isPrefix word4 s then satisfy_aux (remove_prefix word4 s) else
              false in
  satisfy_aux reversed

let main () =
  let s = read_line () in
  let ans = if satisfy s
            then "YES"
            else "NO" in
  (
    Printf.printf "%s" ans;
    print_newline ()
  )

let _ = main ()


(* O(|s|) *)
let s = read_line ()
let i = ref @@ String.length s - 1
let ws = [|"dream"; "dreamer"; "erase"; "eraser"|]
let f b w =
  if b then b
  else let m = String.length w in if String.sub s (max 0 @@ !i - m + 1) m = w then (i := !i - m; true) else b
let _ =
  while !i >= 0 do if not @@ Array.fold_left f false ws then (print_endline "NO"; exit 0) done;
  print_endline "YES"


(* O(|s|) *)
let s = read_line ()
let i = ref @@ String.length s - 1
let ws = [|"dream"; "dreamer"; "erase"; "eraser"|]
let _ =
  while !i >= 0 do
    let f b w =
      if b then b
      else
        let m = String.length w in
        if String.sub s (max 0 @@ !i - m + 1) @@ m = w then (i := !i - m; true) else b in
    if not @@ Array.fold_left f false ws then (print_endline "NO"; exit 0) done;
  print_endline "YES"


(* O(|s|) *)
open Str
let s = read_line ()
let _ = print_endline @@ if string_match (regexp "^\\(dream\\|dreamer\\|erase\\|eraser\\)+$") s 0 then "YES" else "NO"


(* O(|s|) *)
let s = read_line ()
let i = ref @@ String.length s - 1
let ws = [|"dream"; "dreamer"; "erase"; "eraser"|]
let _ =
  while !i >= 0 do
    let j = ref 0 in
    let is_ok = ref false in
    while !j < 4 && not !is_ok do
      let m = String.length ws.(!j) in
      if String.sub s (max 0 @@ !i - m + 1) @@ m = ws.(!j) then (i := !i - m; is_ok := true);
      incr j done;
    if not !is_ok then (print_endline "NO"; exit 0) done;
  print_endline "YES"


(* O(|s|) *)
let solve s =
  let rec loop i =
    if i <= 0 then
      "YES"
    else
      let match_size = List.fold_left
        (fun match_size each ->
          if match_size <> 0 then
            match_size
          else
            let len = String.length each in
            if i - len < 0 then
              match_size
            else
              if String.sub s (i - len) len = each then len else match_size)
        0
        ["dream"; "dreamer"; "erase"; "eraser"]
      in
        if match_size = 0 then
          "NO"
        else
          loop (i - match_size)
  in
    loop (String.length s)

let _ = read_line () |> solve |> print_endline


Scanf.scanf" %s"@@fun s->print_endline@@if Str.(string_match(regexp"^\\s*\\(dream\\|dreamer\\|erase\\|eraser\\)+\\s*$")s 0)then"YES"else"NO"


module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : char -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : char list -> t -> bool
end = struct
  type t = { sort : sort; match_eps : bool; derive : char -> t }
  and sort = EmptySet | EmptyStr | Other

  let rec empty_set =
    { sort = EmptySet;
      match_eps = false;
      derive = fun _ -> empty_set }

  let empty_str =
    { sort = EmptyStr;
      match_eps = true;
      derive = fun _ -> empty_set }

  let char c =
    { sort = Other;
      match_eps = false;
      derive = fun c' -> if c = c' then empty_str else empty_set }

  let rec union re1 re2 =
    match re1, re2 with
    | { sort = EmptySet }, _ -> re2
    | _, { sort = EmptySet } -> re1
    | _, _ ->
        { sort = Other;
          match_eps = re1.match_eps || re2.match_eps;
          derive = fun c -> union (re1.derive c) (re2.derive c) }

  let rec app re1 re2 =
    match re1 with
    | { sort = EmptyStr } -> re2
    | { sort = EmptySet } -> empty_set
    | _ ->
        { sort = Other;
          match_eps = re1.match_eps && re2.match_eps;
          derive = fun c ->
            union
              (app (re1.derive c) re2)
              (if re1.match_eps then re2.derive c else empty_set) }
  let app re1 = function
    | { sort = EmptyStr } -> re1
    | { sort = EmptySet } -> empty_set
    | re2 -> app re1 re2

  let star = function
    | { sort = EmptySet }
    | { sort = EmptyStr } -> empty_str
    | re0 ->
        let rec re =
          { sort = Other;
            match_eps = true;
            derive = fun c -> app (re0.derive c) re } in re

  let matches s re =
    (List.fold_left (fun re -> re.derive) re s).match_eps
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (* ((dream(er)?)|(erase(r)?))* *)
      (Array.to_list @@ Array.init (String.length s) @@ String.get s) @@
      star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))
  then "YES"
  else "NO"


module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : char -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t
  val regex_match : char list -> t -> bool
end = struct
  type t = { match_eps : bool; derive : char -> t }

  let rec empty_set =
    { match_eps = false;
      derive = fun _ -> empty_set }

  let empty_str =
    { match_eps = true;
      derive = fun _ -> empty_set }

  let char c =
    { match_eps = false;
      derive = fun c' -> if c = c' then empty_str else empty_set }

  let rec union re1 re2 =
    { match_eps = re1.match_eps || re2.match_eps;
      derive = fun c -> union (re1.derive c) (re2.derive c) }

  let rec app re1 re2 =
    { match_eps = re1.match_eps && re2.match_eps;
      derive = fun c ->
        union
          (app (re1.derive c) re2)
          (if re1.match_eps then re2.derive c else empty_set) }

  let star re0 =
    let rec re =
      { match_eps = true;
        derive = fun c -> app (re0.derive c) re } in re

  let regex_match s re =
    (List.fold_left (fun re -> re.derive) re s).match_eps
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    regex_match
      (Array.to_list @@ Array.init (String.length s) @@ String.get s) @@
      (* ((dream(er)?)|(erase(r)?))* *)
      star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))
  then "YES"
  else "NO"


module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t =
    { sort : sort;
      is_nullable : bool;
      derive : char -> t }
  and sort =
    | EmptySet
    | EmptyStr
    | Other

  let rec empty_set =
    { sort = EmptySet;
      is_nullable = false;
      derive = fun _ -> empty_set }

  let empty_str =
    { sort = EmptyStr;
      is_nullable = true;
      derive = fun _ -> empty_set }

  let char c =
    { sort = Other;
      is_nullable = false;
      derive = fun c' -> if c = c' then empty_str else empty_set }

  let rec union re1 re2 =
    match re1, re2 with
    | { sort = EmptySet }, _ -> re2
    | _, { sort = EmptySet } -> re1
    | _, _ ->
        { sort = Other;
          is_nullable = re1.is_nullable || re2.is_nullable;
          derive = fun c -> union (re1.derive c) (re2.derive c) }

  let rec app re1 re2 =
    match re1, re2 with
    | { sort = EmptyStr }, _ -> re2
    | { sort = EmptySet }, _ -> empty_set
    | _, _ ->
        { sort = Other;
          is_nullable = re1.is_nullable && re2.is_nullable;
          derive = fun c ->
            union
              (app (re1.derive c) re2)
              (if re1.is_nullable then re2.derive c else empty_set) }

  let star = function
    | { sort = EmptySet }
    | { sort = EmptyStr } -> empty_str
    | re0 ->
        let rec re =
          { sort = Other;
            is_nullable = true;
            derive = fun c -> app (re0.derive c) re } in re

  let matches re s =
    (List.fold_left (fun re -> re.derive) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (* ((dream(er)?)|(erase(r)?))* *)
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"


let memoize n f =
  let dp = Hashtbl.create n in
  let rec get x =
    try Hashtbl.find dp x with
    | Not_found ->
        let result = f get x in
        Hashtbl.add dp x result;
        result in get

module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t =
    { sort : sort;
      is_nullable : bool;
      derive : char -> t }
  and sort =
    | EmptySet
    | EmptyStr
    | Star
    | Other

  let rec empty_set =
    { sort = EmptySet;
      is_nullable = false;
      derive = fun _ -> empty_set }

  let empty_str =
    { sort = EmptyStr;
      is_nullable = true;
      derive = fun _ -> empty_set }

  let char c =
    { sort = Other;
      is_nullable = false;
      derive = fun c' -> if c = c' then empty_str else empty_set }

  let rec union re1 re2 =
    match re1, re2 with
    | { sort = EmptySet }, _ -> re2
    | _, { sort = EmptySet } -> re1
    | _, _ ->
        { sort = Other;
          is_nullable = re1.is_nullable || re2.is_nullable;
          derive = memoize 64 @@ fun _ c -> union (re1.derive c) (re2.derive c) }

  let rec app re1 re2 =
    match re1, re2 with
    | { sort = EmptySet }, _ -> empty_set
    | _, { sort = EmptySet } -> empty_set
    | { sort = EmptyStr }, _ -> re2
    | _, { sort = EmptyStr } -> re1
    | _, _ ->
        { sort = Other;
          is_nullable = re1.is_nullable && re2.is_nullable;
          derive = memoize 64 @@ fun _ c ->
            union
              (if re1.is_nullable then re2.derive c else empty_set)
              (app (re1.derive c) re2) }

  let star = function
    | { sort = EmptySet }
    | { sort = EmptyStr } -> empty_str
    | { sort = Star } as re -> re
    | re ->
        let memo = Hashtbl.create 64 in
        let rec self =
          { sort = Star;
            is_nullable = true;
            derive = fun c ->
              try Hashtbl.find memo c with
              | Not_found ->
                  let d = app (re.derive c) self in
                  Hashtbl.add memo c d;
                  d } in self

  let matches re s =
    (List.fold_left (fun re -> re.derive) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (* ((dream(er)?)|(erase(r)?))* *)
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"


module CharMap = Map.Make (Char)

module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t =
    { body : regexp;
      is_nullable : bool;
      mutable derivatives : t CharMap.t }
  and regexp =
    | EmptySet
    | EmptyStr
    | Char of Char.t
    | App of t * t
    | Union of t * t
    | Star of t

  let empty_set =
    { body = EmptySet;
      is_nullable = false;
      derivatives = CharMap.empty }

  let empty_str =
    { body = EmptyStr;
      is_nullable = true;
      derivatives = CharMap.empty }

  let char c =
    { body = Char c;
      is_nullable = false;
      derivatives = CharMap.empty }

  let app re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> empty_set
    | _, { body = EmptySet } -> empty_set
    | { body = EmptyStr }, _ -> re2
    | _, { body = EmptyStr } -> re1
    | _, _ ->
        { body = App (re1, re2);
          is_nullable = re1.is_nullable && re2.is_nullable;
          derivatives = CharMap.empty }

  let union re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> re2
    | _, { body = EmptySet } -> re1
    | _, _ ->
        { body = Union (re1, re2);
          is_nullable = re1.is_nullable || re2.is_nullable;
          derivatives = CharMap.empty }

  let star = function
    | { body = EmptySet } -> empty_str
    | { body = EmptyStr } -> empty_str
    | { body = Star re } -> re
    | re -> { body = Star re; is_nullable = true; derivatives = CharMap.empty }

  let rec derive c re =
    try CharMap.find c re.derivatives
    with Not_found ->
      let d =
        match re with
        | { body = EmptySet } -> empty_set
        | { body = EmptyStr } -> empty_set
        | { body = Char c' } ->
            if Char.compare c c' = 0 then empty_str else empty_set
        | { body = App (re1, re2) } ->
            union (if re1.is_nullable then derive c re2 else empty_set) (app (derive c re1) re2)
        | { body = Union (re1, re2) } ->
            union (derive c re1) (derive c re2)
        | { body = Star re0 } ->
            app (derive c re0) re in
      re.derivatives <- CharMap.add c d re.derivatives; d

  let matches re s =
    (List.fold_left (fun re c -> derive c re) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"


module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t = { body : regexp; is_nullable : bool }
  and regexp =
    | EmptySet
    | EmptyStr
    | Char of Char.t
    | App of t * t
    | Union of t * t
    | Star of t

  let empty_set =
    { body = EmptySet; is_nullable = false }

  let empty_str =
    { body = EmptyStr; is_nullable = true }

  let char c =
    { body = Char c; is_nullable = false }

  let app re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> empty_set
    | _, { body = EmptySet } -> empty_set
    | { body = EmptyStr }, _ -> re2
    | _, { body = EmptyStr } -> re1
    | _, _ ->
        { body = App (re1, re2);
          is_nullable = re1.is_nullable && re2.is_nullable }

  let union re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> re2
    | _, { body = EmptySet } -> re1
    | _, _ ->
        { body = Union (re1, re2);
          is_nullable = re1.is_nullable || re2.is_nullable }

  let star re =
    { body = Star re; is_nullable = true }

  let rec derive c = function
    | { body = EmptySet } -> empty_set
    | { body = EmptyStr } -> empty_set
    | { body = Char c' } ->
        if Char.compare c c' = 0 then empty_str else empty_set
    | { body = App (re1, re2) } ->
        union (if re1.is_nullable then derive c re2 else empty_set) (app (derive c re1) re2)
    | { body = Union (re1, re2) } ->
        union (derive c re1) (derive c re2)
    | { body = Star re } ->
        app (derive c re) (star re)

  let matches re s =
    (List.fold_left (fun re c -> derive c re) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (* ((dream(er)?)|(erase(r)))* *)
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"


module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t = { body : regexp; is_nullable : bool }
  and regexp =
    | EmptySet
    | EmptyStr
    | Char of Char.t
    | App of t * t
    | Union of t * t
    | Star of t

  let empty_set =
    { body = EmptySet; is_nullable = false }

  let empty_str =
    { body = EmptyStr; is_nullable = true }

  let char c =
    { body = Char c; is_nullable = false }

  let app re1 re2 =
    { body = App (re1, re2);
      is_nullable = re1.is_nullable && re2.is_nullable }

  let union re1 re2 =
    { body = Union (re1, re2);
      is_nullable = re1.is_nullable || re2.is_nullable }

  let star re =
    { body = Star re; is_nullable = true }

  let rec derive c = function
    | { body = EmptySet } -> empty_set
    | { body = EmptyStr } -> empty_set
    | { body = Char c' } ->
        if Char.compare c c' = 0 then empty_str else empty_set
    | { body = App (re1, re2) } ->
        union (if re1.is_nullable then derive c re2 else empty_set) (app (derive c re1) re2)
    | { body = Union (re1, re2) } ->
        union (derive c re1) (derive c re2)
    | { body = Star re } ->
        app (derive c re) (star re)

  let matches re s =
    (List.fold_left (fun re c -> derive c re) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (* ((dream(er)?)|(erase(r)))* *)
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            app (union empty_str @@ app (char 'e') @@ app (char 'r') @@ empty_str) empty_str)
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            app (union empty_str @@ app (char 'r') empty_str) empty_str)) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"


let char_list_of_string str =
  let rec f0 i a =
    if i<0 then a else f0 (i-1) (str.[i]::a)
	in f0 (String.length str - 1) []
let string_of_char_list ls =
	List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls

let () =
	let s = read_line () |> char_list_of_string |> List.rev
	in let hoge a = a|>List.rev|>string_of_char_list
	in let rec f0 a = match a with
	| [] -> "Yes"
	|	p::q::r::s::t::u::v::tl when "dreamer" = hoge [p;q;r;s;t;u;v] -> f0 tl
	| p::q::r::s::t::u::tl when "eraser" = hoge [p;q;r;s;t;u] -> f0 tl
	| p::q::r::s::t::tl when let h = hoge [p;q;r;s;t] in "dream" = h || "erase" = h -> f0 tl
	| _ -> "No"
	in print_endline @@ f0 s


let hd_opt t =
  match t with
  | [] -> None
  | x :: _ -> Some x

open Batteries
let () =
  let s = Scanf.scanf "%s " (fun a -> a) in
  let rev_s = String.rev s in

  let dict = ["dream";"dreamer";"erase";"eraser"] in
  let rev_dict = List.map (fun x -> String.rev x) dict in

  let rec aux s = match s with
      "" -> "YES"
    | s ->
      let res = List.map (fun dict_s ->
          let len = String.length dict_s in
          let target = String.slice ~last:len s in
          if dict_s = target then len else 0
        ) rev_dict |> List.filter (fun x -> x <> 0) in

      match (hd_opt res) with
      Some x -> aux (String.slice ~first:x s)
      | None -> "NO"
  in

  Printf.printf "%s\n" @@
  aux rev_s


open Batteries
let () =
  let s = Scanf.scanf "%s " (fun a -> a) in
  let rev_s = String.rev s in

  let dict = ["dream";"dreamer";"erase";"eraser"] in
  let rev_dict = List.map (fun x -> String.rev x) dict in

  let rec aux s = match s with
      "" -> "YES"
    | s ->
      let res = List.map (fun dict_s -> dict_s =  String.slice ~last:(String.length dict_s) s) rev_dict |>
                List.filter (fun x -> x = true) |> List.length in
      if res = 0 then "NO" else aux (String.slice ~first:(String.length s) s)
  in

  Printf.printf "%s\n" @@
  aux rev_s


let l = ["dream"; "dreamer"; "erase"; "eraser"]

let has_suffix s suffix =
  let lens = String.length s in
  let len_suf = String.length suffix in
  if lens < len_suf then false
  else String.sub s (lens - len_suf) len_suf = suffix

let trim_suffix s suffix =
  let lens = String.length s in
  let len_suf = String.length suffix in
  String.sub s 0 (lens - len_suf)

let rec f s =
  if s = "" then "YES"
  else
    match List.filter (has_suffix s) l with
    | [] -> "NO"
    | suf -> f (trim_suffix s (List.hd suf))

let () = f (read_line ()) |> print_endline


let list_of_str s = let rec f i = if i >= String.length s then [] else s.[i] :: f (i+1) in f 0
let rec solve = function
  | [] -> "YES"
  | ('r'::'e'::('m'::'a'::'e'::'r'::'d'::xs | 's'::'a'::'r'::'e'::xs))
  | ('m'::'a'::'e'::'r'::'d'::xs) | ('e'::'s'::'a'::'r'::'e'::xs)  -> solve xs
  | _ -> "NO"
let () = Scanf.scanf "%s" list_of_str |> List.rev |> solve |> print_endline


let main () =
  let n = Scanf.scanf "%s\n" (fun a -> a) in
  let n = ref @@ BatString.rev n in
  let xs = BatArray.map BatString.rev [|"dream"; "dreamer"; "erase"; "eraser"|] in
  let flag = ref true in
  while !flag do
    flag := BatArray.fold_left (fun acc elm ->
        if BatString.equal (BatString.left !n @@ BatString.length elm) elm
        then (n := BatString.tail !n @@ BatString.length elm; true)
        else acc || false
      ) false xs
  done;
  Printf.printf (if BatString.is_empty !n then "YES" else "NO")

let _ = main ()


let rev s =
  let len = String.length s in
  let rec aux i acc =
    if i = len then acc else aux (i + 1) (Char.escaped s.[i] ^ acc)
  in
  aux 0 ""

let has_any_prefix s =
  try
    List.find
      (fun word ->
         let len = String.length word in
         try String.sub s 0 len = word with _ -> false)
      ["maerd"; "remaerd"; "esare"; "resare"]
  with Not_found -> ""

let trim_prefix s prefix =
  let len_pre = String.length prefix in
  let len_s = String.length s in
  String.sub s len_pre (len_s - len_pre)

let rec solve str =
  if String.length str = 0 then "YES"
  else
    match has_any_prefix str with
    | "" -> "NO"
    | a -> trim_prefix str a |> solve

let () = read_line () |> rev |> solve |> print_endline


let rev s =
  let len = String.length s in
  let rec aux i acc =
    if i = len then acc else aux (i + 1) (Char.escaped s.[i] ^ acc)
  in
  aux 0 ""

let has_any_prefix s =
  try
    List.find
      (fun word ->
        let len = String.length word in
        try String.sub s 0 len = word with _ -> false)
      ["maerd"; "remaerd"; "esare"; "reesare"]
  with Not_found -> ""

let trim_prefix s prefix =
  let len_pre = String.length prefix in
  let len_s = String.length s in
  String.sub s len_pre (len_s - len_pre)

let rec solve str =
  if String.length str = 0 then "YES"
  else
    match has_any_prefix str with
    | "" -> "NO"
    | a -> trim_prefix str a |> solve

let () = read_line () |> rev |> solve |> print_endline


Scanf.scanf "%s" (fun s ->
    let n = String.length s in
    let rec loop i prev acc =
        if i = n then acc else
            if s.[i] = prev then loop (i + 1) prev acc else
                loop (i + 1) s.[i] (acc + 1)
    in
    loop 1 s.[0] 0 |> Printf.printf "%d\n"
)

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


let ans s =
  let n = String.length s in
  let rec f i c ret =
    if i = n then ret
    else if s.[i] = c then f (i+1) c ret
    else f (i+1) s.[i] (ret+1)
  in f 1 s.[0] 0;;

Scanf.scanf "%s" (fun s -> print_int (ans s))


let s = read_line ()
let ans, p = ref 0, ref s.[0]
let _ = String.iter (fun c -> if c <> !p then incr ans; p := c) s; Printf.printf "%d\n" !ans


open Batteries
let rec solve acc = function
  | (x::y::ys) ->
      let acc' = acc + if x = y then 0 else 1 in
      solve acc' (y::ys)
  | _ -> acc
let () =
  Scanf.scanf "%s" String.to_list |> solve 0 |> Printf.printf "%d\n"


let solve s =
  let len = String.length s in
  let rec aux i pre count =
    if i = len then count
    else if pre <> s.[i] then aux (i + 1) s.[i] (count + 1)
    else aux (i + 1) pre count
  in
  aux 0 s.[0] 1

let () = solve (read_line ()) - 1 |> Printf.printf "%d\n"


let one_color s =
  not (String.contains s 'W' && String.contains s 'B')

let flip s =
  let len = String.length s in
  let newcolor = if s.[(len-1)] = 'W' then 'B' else 'W' in
  let rec aux i =
    if i = -1 then String.make len newcolor
    else if s.[i] <> newcolor then aux (i-1)
    else (String.sub s 0 (i+1)) ^ (String.make (len - i) newcolor)
  in
  aux (len - 1)

let rec solve s count =
  if one_color s then count
  else solve (flip s) (count + 1)

let () =
  let s = Scanf.scanf "%s" (fun s -> s) in
  Printf.printf "%d\n" (solve s 0)


let rec solver lst =
	match lst with
	| [] -> 0
	| _::[] -> 0
	| x::y::xs ->
		if x = y
		then 0 + solver (y::xs)
		else 1 + solver (y::xs)

let _ = Scanf.scanf "%s\n" (fun x -> x)
		|> Extlib.ExtString.String.to_list
		|> solver
		|> Printf.printf "%d"


module String = struct
  include String
  let to_list s =
    let rec loop acc = function
      | 0 -> acc
      | n -> loop (s.[n - 1] :: acc) (n - 1) in
    loop [] (String.length s)
end

let rec tabulate n f =
  let rec loop acc m =
    if m < n then loop (f m :: acc) (m + 1)
    else List.rev acc in
  loop [] 0

let () =
  read_line ()
    |> String.to_list
    |> List.fold_left (fun (prev, n) c ->
        (c, if prev = c then n else n + 1)) ('X', 0)
    |> (fun (_, n) -> n - 1)
    |> Printf.printf "%d\n"


let r_long () = Int64.of_int (Scanf.scanf " %d" (fun x -> x)) ;;
let r_int () = Scanf.scanf " %d" (fun x -> x) ;;
let n=r_int() in
let l=r_int() in
let a=Array.make n "" in
for i=0 to n-1 do
let s=read_line() in
a.(i)<-s;
done;
Array.sort String.compare a;
for i=0 to n-1 do
print_string a.(i);
done;
print_newline();

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


let rec read_lines () =
	try let line = read_line () in line :: read_lines ()
    with End_of_file -> []
let n, l = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let s = List.fast_sort compare (read_lines ())
let ans = List.fold_left (^) "" s


let n, l = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init n @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let _ = Array.(sort compare ss; iter print_string ss); print_newline ()


let n, l = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init n @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let _ = Array.(sort compare ss; to_list ss |> String.concat "" |> print_endline)


(* O(l n^2) *)
Scanf.scanf "%d %d" @@ fun n l ->
  let ss = Array.init n @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s in
  Array.sort compare ss;
  Array.fold_left (^) "" ss |> print_endline


open Batteries

let () =
  let n = Scanf.scanf " %d %d\n" (fun n _ -> n) in
  List.init n (fun _ -> Scanf.scanf "%s\n" (fun x -> x))
  |> List.sort compare
  |> List.fold_left (fun x y -> x ^ y) ""
  |> print_endline


open Batteries

let () =
  let n = Scanf.scanf " %d %d\n" (fun n _ -> n) in
  List.init n (fun _ -> read_line ())
  |> List.sort compare
  |> List.fold_left (fun x y -> x ^ y) ""
  |> print_endline


open Batteries
open Printf
let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let l = Scanf.scanf "%d " (fun a -> a) in
  let s_l = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%s " (fun a -> a)) in

  let s_ls = List.sort compare s_l in

  Printf.printf "%s\n" @@
  List.fold_left (^) "" s_ls


let n, _ = Scanf.scanf "%d %d\n" (fun x y -> x, y)

let mkls i =
  Array.init i (fun _ -> Scanf.scanf "%s\n" (fun s -> s))
  |> Array.to_list

let cat ls =
  let rec aux tmp = function
    | [] -> tmp
    | h :: t -> aux (tmp ^ h) t
  in
  aux "" ls

let () = mkls n |> List.sort String.compare |> cat |> print_endline


open Scanf
let () =
  let xs = scanf "%d %d" (fun n l -> Array.init n (fun _ -> scanf " %s" (fun s -> s))) in
  Array.sort compare xs;
  Array.iter (Printf.printf "%s") xs;
  print_newline ()


let (n, l) = Scanf.scanf "%d %d\n" (fun a b -> a, b)
let s = Array.make n ""

let () =
  for i = 0 to n-1 do
    s.(i) <- read_line ()
  done;
  Array.sort compare s;
  for i = 0 to n-1 do
    print_string s.(i)
  done;
  print_newline ()


(* let sl = Str.split (Str.regexp " ") @@ read_line () *)
(* let il = List.map int_of_string sl *)
(* let ar = Array.of_list il *)

(* let (a, b) = Scanf.scanf "%d %d\n" (fun a b -> a, b) *)
(* let () = Printf.printf "%d %f\n" 2 3.5 *)

let (n, l) = Scanf.scanf "%d %d\n" (fun a b -> a, b)
let s = Array.make n ""

let () =
  for i = 0 to n-1 do
    s.(i) <- read_line ()
  done

let () =
  Array.sort compare s;
  for i = 0 to n-1 do
    print_string s.(i)
  done;
  print_newline ()


let (n, l) = Scanf.scanf "%d %d\n" (fun a b -> a, b)
let s = Array.make n ""

let () =
  for i = 0 to n-1 do
    s.(i) <- read_line ()
  done

let () =
  Array.sort compare s;
  for i = 0 to n-1 do
    print_string s.(i)
  done;
  print_newline ()


let make_list f t step =
  let rec make_list_inner c t lst =
    if c >= t then lst else make_list_inner (c+step) t (c::lst)
  in
  List.rev (make_list_inner f t [])

let print_list str list = Printf.printf "%s: [ " str; List.iter (fun (q,r) -> Printf.printf "(%d, %d); " q r) list; Printf.printf "]\n"

let () =
  let rec read lst =
    try let ans = read_line () in
        read (ans::lst)
    with End_of_file ->
      let lst = List.sort (compare) lst in
      Printf.printf "%s\n" (List.fold_left (^) "" lst)
  in
  ignore (read_line ());
  read []
;;


let main = Scanf.sscanf (read_line()) "%d %d" (fun n l ->
    let rec inputdata n =
        if n = 0
        then []
        else read_line() :: inputdata (n - 1)
    in let rec sortdata lst = List.sort compare lst
    in let rec printdata lst =
        match lst with
        | [] -> ()
        | first :: lst -> (
            print_string first;
            printdata lst
        )
    in printdata (sortdata (inputdata n));
    print_string "\n"
)


let main = Scanf.sscanf (read_line()) "%d %d" (fun n l ->
    let rec inputdata n =
        if n = 0
        then []
        else read_line() :: inputdata (n - 1)
    in let rec sortdata lst =
        match lst with
        | [] -> []
        | first :: lst -> List.filter ((>) first) lst @ [first] @ List.filter ((<=) first) lst
    in let rec printdata lst =
        match lst with
        | [] -> ()
        | first :: lst -> (
            print_string first;
            printdata lst
        )
    in printdata (sortdata (inputdata n));
    print_string "\n"
)


let n = Scanf.scanf "%d %d" (fun n l -> n)

let str =
	let rec loop lst n =
		if n = 0 then lst
		else loop ((read_line ()) :: lst) (n - 1)
	in
	loop [] n

let () =
	List.sort compare str
	|> String.concat ""
	|> print_endline


let rec tabulate n f =
  let rec loop acc m =
    if m < n then loop (f m :: acc) (m + 1)
    else List.rev acc in
  loop [] 0

let () =
  let n, l = Scanf.scanf "%d %d\n" (fun n l -> n, l) in
  let ss = tabulate n (fun _ -> Scanf.scanf "%s\n" (fun s -> s)) in
  List.sort compare ss
  |> List.fold_left ( ^ ) ""
  |> print_endline