Skip to content

Instantly share code, notes, and snippets.

@sizumita
Created June 7, 2020 12:59
Show Gist options
  • Save sizumita/38e7541a7be67c3997f421c30c3fd838 to your computer and use it in GitHub Desktop.
Save sizumita/38e7541a7be67c3997f421c30c3fd838 to your computer and use it in GitHub Desktop.
let n = read_int ()
let n2 = n / 2
(* 文字列をcharのlistにする *)
let explode s = List.init (String.length s) (String.get s)
let is_even = (if n mod 2 = 1 then true else false)
let a = Array.init n @@ fun _ -> read_line () |> explode
let rec main_loop i result =
if i >= n2 then result else
let rec loop lst =
match lst with
| [] -> '0'
| first :: rest -> if List.mem first a.(n-1-i) then first else loop rest
in let p = loop a.(i) in
if p = '0' then [] else
main_loop (i+1) (p :: result)
(* charのlistをstringに *)
let string_of_chars chars =
let buf = Buffer.create 16 in
List.iter (Buffer.add_char buf) chars;
Buffer.contents buf
let r = main_loop 0 []
let _ = (
if n = 1 then string_of_chars [List.hd a.(0)] else
if r = [] then "-1" else
if is_even then string_of_chars ((List.rev r) @ [List.hd a.(n2)] @ r) else
string_of_chars ((List.rev r) @ r)) |> print_endline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment